package com.tudouji.framework.task;


import com.tudouji.common.enums.*;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.bill.domain.*;
import com.tudouji.project.bill.service.*;
import com.tudouji.project.copy.domain.Copyparam;
import com.tudouji.project.copy.domain.Copytokd;
import com.tudouji.project.copy.domain.Copytokditem;
import com.tudouji.project.copy.domain.MultipartConfig;
import com.tudouji.project.copy.service.ICopyparamService;
import com.tudouji.project.copy.service.ICopytokdService;
import com.tudouji.project.copy.service.ICopytokditemService;
import com.tudouji.project.copy.service.IMultipartConfigService;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.service.ICustomerService;
import com.tudouji.project.harvest.service.IFreightorderService;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.domain.Supplier;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.project.organization.service.ISupplierService;
import com.tudouji.project.product.domain.Innerprice;
import com.tudouji.project.product.domain.Productinfo;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.service.IInnerpriceService;
import com.tudouji.project.product.service.IProductinfoService;
import com.tudouji.project.product.service.ISalesspecificationService;
import com.tudouji.project.store.domain.Stockunit;
import com.tudouji.project.store.domain.Storehouseroom;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStockunitService;
import com.tudouji.project.store.service.IStorehouseroomService;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.SysConfig;
import com.tudouji.project.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  金蝶数据同步 第一步：生成待同步数据
 */
@Slf4j
@Component("laterTask")
public class LaterTask {

    @Autowired
    private IWhoutformService whoutformService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ICopytokdService copytokdService;

    @Autowired
    private IWhoutformitemService whoutformitemService;

    @Autowired
    private ICopytokditemService copytokditemService;

    @Autowired
    private IMultipartConfigService multipartConfigService;

    @Autowired
    private ICopyparamService copyparamService;

    @Autowired
    private ISellformService sellformService;

    @Autowired
    private IOrganizationService organizationService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IStoreroomService storeroomService;

    @Autowired
    private IStorehouseroomService storehouseroomService;

    @Autowired
    private ISalesspecificationService salesspecificationService;

    @Autowired
    private IWhinformService whinformService;

    @Autowired
    private IStockunitService stockunitService;

    @Autowired
    private IProducereportService producereportService;

    @Autowired
    private ITransferapplyitemService transferapplyitemService;

    @Autowired
    private ITransferapplyService transferapplyService;

    @Autowired
    private ISellitemService sellitemService;

    private List<Customer> customerList = new ArrayList<Customer>();

    private List<Supplier> supplierList = new ArrayList<Supplier>();

    private List<Storehouseroom> storehouseroomList = new ArrayList<Storehouseroom>();

    private List<Storeroom> storeroomList = new ArrayList<Storeroom>();

    private List<Salesspecification> salesspecificationList = new ArrayList<Salesspecification>();

    private List<Stockunit> stockunitList = new ArrayList<Stockunit>();


    /**
     *
     * 非标品内部结算价（比如7.5/公斤）参数，做为样品、赠送、促销订单的内部结算价
     */
    private BigDecimal innPrice;

    /**
     * 内部折扣
     *
     */
    private BigDecimal innDiscount;


    private String kdncode = null;

    private static int i = 1;

    public void init() {
        customerList = this.customerService.selectCustomerList(new Customer());
        supplierList = this.supplierService.selectSupplierList(new Supplier());
        storehouseroomList = this.storehouseroomService.selectStorehouseroomList(new Storehouseroom());
        storeroomList = this.storeroomService.selectStoreroomList(new Storeroom());
        stockunitList = this.stockunitService.selectStockunitList(new Stockunit());
        salesspecificationList = this.salesspecificationService.selectSalesspecificationList(new Salesspecification());

    }

    public void initInnPrice(){
        String price = this.sysConfigService.selectConfigByKey("inner_price");
        if(StringUtils.isNotEmpty(price)){
            innPrice = new BigDecimal(price);
        }
    }

    public void initInnDiscount(){
        String dis = this.sysConfigService.selectConfigByKey("inner_discount");
        if(StringUtils.isNotEmpty(dis)){
            innDiscount = new BigDecimal(dis);
        }
    }


    /**
     * 待同步数据
     */
    @Transactional
    public void laterform() throws Exception {
        log.debug("===============拉取数据开始====================");
        String proess = sysConfigService.selectConfigByKey("job_process");
        if ("1".equals(proess) || "3".equals(proess) || "5".equals(proess)) {
            return;
        }

        try {

            SysConfig sysConfig = this.sysConfigService.selectConfigById(4L);
            sysConfig.setConfigValue("1");
            this.sysConfigService.updateConfig(sysConfig);

            init();

            initInnPrice();

            initInnDiscount();

            Copyparam copyparam = new Copyparam();
            copyparam.setIscopy(IsCopyEnums.YES.getCode());
            copyparam.setKind(KindEnums.BILL.getCode());
            List<Copyparam> copyparamList = this.copyparamService.selectCopyparamList(copyparam);

            for (Copyparam c : copyparamList) {
                //入库单
                if (StringUtils.isNotEmpty(c.getIntype())) {
                    this.laterInform(c.getIntype(), c.getStartdate(), c);
                }
                //出库单
                if (StringUtils.isNotEmpty(c.getOuttype())) {
                    this.laterOutform(c.getOuttype(), c.getStartdate(), c);
                }
                if (StringUtils.isEmpty(c.getIntype()) && StringUtils.isEmpty(c.getOuttype())) {
                    if (c.getType().equals(CopyTypeEnums.PRODUCTION_OUT.getCode())) {
                        //生产出库，从生产报表中拉去数据
                        //拉去成功后将生产计划的状态置成5
                        productionOut(c.getStartdate(), c);
                    } else if (c.getType().equals(CopyTypeEnums.REDEPLOY.getCode())) {
                        //调拨出入库
                        //查询已完成的调拨单，然后按照调拨单明细查询写入待处理，并且将调拨单的Iscopy字段更新为1
                        redeploy(c);

                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            log.debug(e.getMessage());
        } finally {
            SysConfig sysConfig1 = this.sysConfigService.selectConfigById(4L);
            sysConfig1.setConfigValue("2");
            this.sysConfigService.updateConfig(sysConfig1);

        }
    }

    /**
     * 调拨出入库
     */
    @Transactional
    public void redeploy(Copyparam copyparam) {
        Date date = new Date();
//        String diff = this.sysConfigService.selectConfigByKey("tran_diff");
//        if(StringUtils.isEmpty(diff)){
//            log.error("没有找到调拨单差异");
//        }
        Transferapplyitem transferapplyitem = new Transferapplyitem();
        transferapplyitem.setStatus(30);
        transferapplyitem.setIscopys(new Integer[]{0,2});
        transferapplyitem.setType(1);
        transferapplyitem.setCreateTime(copyparam.getStartdate());
        List<Transferapplyitem> transferapplyList = this.transferapplyitemService.selectTransferapplyitemList(transferapplyitem);
        for(Transferapplyitem t : transferapplyList){

            if(t.getTransferapply().getIscopy() == 2){
                //推送过
                Copytokd copytokd = new Copytokd();
                copytokd.setCode(t.getTransferapply().getCode());
                List<Copytokd> copytokdList = this.copytokdService.selectcopytokdAllList(copytokd);
                for(Copytokd c : copytokdList){
                    if(c.getStatus() == CopyStatusEnums.SUCCESS.getCode()){
                        if(c.getTotalqty().compareTo(BigDecimal.ZERO) == 1){
                            //同步成功，生成反向
                            reverseTransferapply(c);
                        }else{
                            break;
                        }
                    }else{
                        this.copytokdService.deleteCopytokdById(c.getId());
                    }
                }
            }

            Whoutform whoutform = new Whoutform();
            whoutform.setBusinesscode(t.getTransferapply().getCode());
            whoutform.setNotisCopytoKD(2);
            List<Whoutform> whoutformList = this.whoutformService.selectWhoutformList(whoutform);

            for(int o = whoutformList.size() - 1; o >= 0; o--){
                Whoutform wof = whoutformList.get(o);
                if(wof.getWeight().compareTo(BigDecimal.ZERO) == -1){
                    //小于0 跳出
                    break;
                }
                Whinform whinform = new Whinform();
                whinform.setBusinesscode(t.getTransferapply().getCode());
                whinform.setNotisCopytoKD(2);
                List<Whinform> whinformList = this.whinformService.selectWhinformList(whinform);

                if(whinformList.size() > 0){
                    if(t.getTransferapply().getOrgid().equals(t.getTransferapply().getToorgid())){
                        //同组织
                        redeployIn(whinformList.get(0), date,t.getUnloadweight(),1,t);
                        redeployOut(whoutformList.get(0), date, t.getLoadweight(),1,t);
                    }else{
                        //跨组织
                        //入库大于出库
                        if(t.getUnloadweight().compareTo(t.getLoadweight())  == 1){
                            redeployIn(whinformList.get(0), date,t.getLoadweight(),1,t);
                            redeployOut(whoutformList.get(0), date, t.getLoadweight(),1,t);
                            redeployIn(whinformList.get(0), date,t.getUnloadweight().subtract(t.getLoadweight()),3,t);
                        }else if(t.getUnloadweight().compareTo(t.getLoadweight()) == -1){
                            //入库小于出库
                            if(copyparam.getParam().equals("1")){
                                //调入方承担
                                redeployIn(whinformList.get(0), date,t.getLoadweight(),1,t);
                                redeployOut(whoutformList.get(0), date, t.getLoadweight(),1,t);
                                redeployIn(whinformList.get(0), date,t.getLoadweight().subtract(t.getUnloadweight()),2,t);
                            }else{
                                //调出方承担
                                redeployIn(whinformList.get(0), date,t.getUnloadweight(),1,t);
                                redeployOut(whoutformList.get(0), date, t.getUnloadweight(),1,t);
                                redeployOut(whoutformList.get(0), date, t.getLoadweight().subtract(t.getUnloadweight()),2,t);
                            }
                        }else{
                            redeployIn(whinformList.get(0), date,t.getUnloadweight(),1,t);
                            redeployOut(whoutformList.get(0), date, t.getLoadweight(),1,t);
                        }
                    }
                }

            }
            Transferapply transferapply = new Transferapply();
            transferapply.setGuid(t.getTransferapply().getGuid());
            transferapply.setIscopy(1);
            this.transferapplyService.updateTransferapply(transferapply);
        }
    }


    /**
     * 反向生成待同步数据
     *
     * @param copytokd
     */
    @Transactional
    public void reverseTransferapply(Copytokd copytokd){
        copytokd.setTotalqty(BigDecimal.ZERO.subtract(copytokd.getTotalqty()));
        //copytokd.setTotalamount(BigDecimal.ZERO.subtract(copytokd.getTotalamount()));
        copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        copytokd.setKdcode(copytokd.getCode() + getI(copytokd.getCode()));
        copytokd.setPushdate(null);
        this.copytokdService.insertCopytokd(copytokd);
        log.debug(copytokd.getId() + "");


        //查询明细数据
        Copytokditem copytokditem=new Copytokditem();
        copytokditem.setCtkdid(copytokd.getId());
        List<Copytokditem> items=copytokditemService.selectCopytokditemList(copytokditem);

        for(Copytokditem item : items){
            item.setCtkdid(copytokd.getId());
            item.setQty(BigDecimal.ZERO.subtract(item.getQty()));
            item.setKdqty(BigDecimal.ZERO.subtract(item.getKdqty()));
            item.setAmount(BigDecimal.ZERO.subtract(item.getAmount()));
            item.setStatus(CopyStatusEnums.INIT.getCode());
            this.copytokditemService.insertCopytokditem(item);
        }
    }


    /**
     * 待同步入库单
     *
     * @throws Exception
     */

    public void laterInform(String types, Date startdate, Copyparam copyparam) throws Exception {
        // List<Supplier> supplierList = this.supplierService.selectSupplierList(new Supplier());
        Date date = DateUtils.getNowDate();
        Copytokd copytokd = new Copytokd();
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtypes(types);

        Long autoid = this.copytokdService.getMaxlinkedid(copytokd);

        Whinform whinform = new Whinform();
        if (StringUtils.isNotEmpty(copyparam.getConditions())) {
            whinform.setConditions(copyparam.getConditions());
        }
        whinform.setTypes(types);
        whinform.setNotisCopytoKD(2);
        if (autoid > 0) {
            whinform.setAutoid(autoid);
        } else {
            whinform.setBeginCreatedate(startdate);
            whinform.setEndCreatedate(DateUtils.getDateBeforeMinite(date, -3));
        }

        List<Whinform> whinformList = this.whinformService.selectWhinformList(whinform);
        if (whinformList.size() > 0) {
            for (Whinform wif : whinformList) {
                if(wif.getWhinformitemList().size() > 0){
                    if (copyparam.getType() == CopyTypeEnums.REDEPLOY.getCode()) {
                        //调拨入库
                        //redeployIn(wif, date);
                    } else if (copyparam.getType() == CopyTypeEnums.PURCHASING.getCode()) {
                        //采购入库
                        purchasingIn(wif, date);
                    } else if (copyparam.getType() == CopyTypeEnums.PRODUCTION.getCode()) {
                        //生产入库
                        productionIn(wif, date);
                    } else if (copyparam.getType() == CopyTypeEnums.JUSTING.getCode()) {
                        //调整入库
                        justingIn(wif, date);
                    } else if (copyparam.getType() == CopyTypeEnums.BACK.getCode()) {
                        //退料入库
                        backIn(wif, date);
                    } else if (copyparam.getType() == CopyTypeEnums.HARVEST.getCode()) {
                        //采收入库
                        harvestn(wif, date);
                    } else {
                        log.info("没有找到入库类型的数据");
                    }
                }

            }

        }
    }

    /**
     * 待同步出库单
     *
     * @throws Exception
     */
    @Transactional
    public void laterOutform(String types, Date startdate, Copyparam copyparam) throws Exception {

        Date date = DateUtils.getNowDate();


        Copytokd copytokd = new Copytokd();
        //同步数据类型 为出库单
        copytokd.setType(BillTypeEnums.OUT.getCode());
        //子类型 为出库单
        copytokd.setSubtypes(types);
        //获取已同步的 最大autoid 
        Long autoid = this.copytokdService.getMaxlinkedid(copytokd);
        
        Whoutform whoutform = new Whoutform();
        if (StringUtils.isNotEmpty(copyparam.getConditions())) {
            whoutform.setConditions(copyparam.getConditions());
        }
        whoutform.setTypes(types);
        whoutform.setNotisCopytoKD(2);
        if (autoid > 0) {
            whoutform.setAutoid(autoid);
        } else {
            whoutform.setBeginCreatedate(startdate);
            whoutform.setEndCreatedate(DateUtils.getDateBeforeMinite(date, -3));
        }
        List<Whoutform> whoutformList = this.whoutformService.selectWhoutformList(whoutform);
        
        
        for (Whoutform wof : whoutformList) {
            if(wof.getWhoutformitemList().size() > 0){
                if (copyparam.getType() == CopyTypeEnums.SALE.getCode()) {
                    //处理销售出库
                    saleOut(wof, date);
                } else if (copyparam.getType() == CopyTypeEnums.REDEPLOY.getCode()) {
                    //调拨出库
                    //redeployOut(wof, date);
                } else if (copyparam.getType() == CopyTypeEnums.PRODUCTION.getCode()) {
                    //生产领料出库
                    //productionOut(wof, date);
                } else if (copyparam.getType() == CopyTypeEnums.JUSTING.getCode()) {
                    //调整出库
                    justingOut(wof, date);
                } else if (copyparam.getType() == CopyTypeEnums.SCRAPPING.getCode()) {
                    //报废出库
                    scrappingOut(wof, date);
                } else {
                    log.info("没有找到出库类型的数据");
                }
            }
        }
    }

    /**
     * 退料入库单
     *
     * @param wif
     * @param date
     */
    @Transactional
    public void backIn(Whinform wif, Date date) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setKdcode();   //应该没用
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        copytokd.setKdtype(KDWhinformTypeEnums.TAKEOUT.getCode());
        copytokd.setSrguid(wif.getStorermguid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(wif.getQty());
        copytokd.setTotalamount(wif.getPrice());
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();
        if (wif.getWhinformitemList() != null) {
            for (Whinformitem item : wif.getWhinformitemList()) {
                boolean flagitem = true;
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(copyId);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(item.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                copytokditem.setQty(item.getWeight());
                getQtyIn(copytokditem, item);
                copytokditem.setSaleorgid(wif.getSettlOrgId());
                //copytokditem.setKdqty();//?????
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                // copytokditem.setCurrency(item.get); //币种没有
                //copytokditem.set
                copytokditem.setCreatedate(date);
                copytokditem.setCopyTime(date);
                if (flagitem) {
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }

    /**
     * 调拨入库单
     *
     * @param wif
     * @param date
     * @param qty
     * @param type 1原始   2其他
     */
    @Transactional
    public void redeployIn(Whinform wif, Date date,BigDecimal qty,Integer type,Transferapplyitem t) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setKdcode();   //应该没用
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        if(type == 1){
            if (wif.getOrgguid().equals(wif.getSettlOrgId())) {
                //同组织
                copytokd.setKdtype(KDWhinformTypeEnums.OTHER.getCode());
            } else {
                //跨组织
                copytokd.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
            }
        }else if( type == 2){
            copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        }else{
            copytokd.setKdtype(KDWhinformTypeEnums.OTHER.getCode());
        }
        copytokd.setSrguid(wif.getStorermguid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(qty);
        copytokd.setTotalamount(wif.getPrice());
        List<Supplier> suppliers = supplierList.stream().filter(s -> wif.getSettlOrgId().equals(s.getOrgguid())).collect(Collectors.toList());
        if (suppliers.size() == 1) {
            copytokd.setSupplierid(suppliers.get(0).getId());
        } else {
            flag = false;
            copytokd.setCopyRemark(copytokd.getCopyRemark() + "没有找到供应商,");
        }
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();


        boolean flagitem = true;
        Copytokditem copytokditem = new Copytokditem();
        copytokditem.setCtkdid(copyId);
        copytokditem.setSrguid(t.getTransferapply().getTosrguid());
        copytokditem.setShrguid(t.getToshguid());
        copytokditem.setOrgguid(t.getTransferapply().getToorgid());
        copytokditem.setOrgname(t.getTransferapply().getToorgname());
        copytokditem.setProductguid(t.getProductguid());
        copytokditem.setSpguid(t.getSpguid());
        copytokditem.setQty(qty);
        //getQtyIn(copytokditem, item);
        if (wif.getOrgguid().equals(wif.getSettlOrgId())) {
            copytokditem.setPrice(BigDecimal.ZERO);
        }else{
            copytokditem.setPrice(t.getInterSettlPrice());
        }

        copytokditem.setSaleorgid(wif.getSettlOrgId());
        //copytokditem.setKdqty();//?????
        copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
        copytokditem.setUnit(t.getStockunit());
        copytokditem.setUnitid(t.getStockunitid());
        List<Supplier> suppliersitem = supplierList.stream().filter(s -> wif.getSettlOrgId().equals(s.getOrgguid())).collect(Collectors.toList());
        if (suppliersitem.size() == 1) {
            copytokditem.setSupplierid(suppliersitem.get(0).getId());
            copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
        } else {
            flagitem = false;
            copytokditem.setCopyRemark(copytokd.getCopyRemark() + "没有找到供应商,");
        }
        // copytokditem.setCurrency(item.get); //币种没有
        //copytokditem.set
        copytokditem.setCreatedate(date);
        copytokditem.setCopyTime(date);
        if (flagitem) {
            copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokditemService.insertCopytokditem(copytokditem);

//        if (wif.getWhinformitemList() != null) {
//            for (Whinformitem item : wif.getWhinformitemList()) {
//                boolean flagitem = true;
//                Copytokditem copytokditem = new Copytokditem();
//                copytokditem.setCtkdid(copyId);
//                copytokditem.setSrguid(item.getSrguid());
//                copytokditem.setShrguid(item.getShrguid());
//                copytokditem.setOrgguid(item.getOrgguid());
//                copytokditem.setOrgname(item.getOrgname());
//                copytokditem.setProductguid(item.getProductguid());
//                if (StringUtils.isNotEmpty(item.getSpguid())) {
//                    copytokditem.setSpguid(item.getSpguid());
//                } else {
//                    copytokditem.setSpguid(item.getRawspguid());
//                }
//                copytokditem.setQty(qty);
//                getQtyIn(copytokditem, item);
//                copytokditem.setPrice(t.getInterSettlPrice());
//                copytokditem.setSaleorgid(wif.getSettlOrgId());
//                //copytokditem.setKdqty();//?????
//                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
//                copytokditem.setUnit(item.getStockunit());
//                copytokditem.setUnitid(item.getStockunitid());
//                List<Supplier> suppliersitem = supplierList.stream().filter(s -> wif.getSettlOrgId().equals(s.getOrgguid())).collect(Collectors.toList());
//                if (suppliersitem.size() == 1) {
//                    copytokditem.setSupplierid(suppliersitem.get(0).getId());
//                    copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
//                } else {
//                    flagitem = false;
//                    copytokditem.setCopyRemark(copytokd.getCopyRemark() + "没有找到供应商,");
//                }
//                // copytokditem.setCurrency(item.get); //币种没有
//                //copytokditem.set
//                copytokditem.setCreatedate(date);
//                copytokditem.setCopyTime(date);
//                if (flagitem) {
//                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
//                } else {
//                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
//                }
//                this.copytokditemService.insertCopytokditem(copytokditem);
//            }
//        }
    }


    /**
     * 采购入库
     *
     * @param wif
     * @param date
     */
    @Transactional
    public void purchasingIn(Whinform wif, Date date) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        copytokd.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(wif.getQty());
        copytokd.setTotalamount(wif.getPrice());
        copytokd.setSupplierid(wif.getSupplierId());
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();
        if (wif.getWhinformitemList() != null) {
            for (Whinformitem item : wif.getWhinformitemList()) {
                boolean flagitem = true;
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(copyId);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(item.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                //copytokditem.setQty(item.getWeight());
                copytokditem.setAmount(item.getAmount());
                copytokditem.setQty(item.getWeight());
                getQtyIn(copytokditem, item);
                if(copytokditem.getKdqty() != null){
                    copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(),6, RoundingMode.HALF_UP));
                }
                copytokditem.setSaleorgid(wif.getSettlOrgId());
                //copytokditem.setPrice(item.getPrice());
                //copytokditem.setKdqty();//?????
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                copytokditem.setSupplierid(wif.getSupplierId());
                copytokditem.setBalancesupplierid(wif.getSupplierId());
                // copytokditem.setCurrency(item.get); //币种没有
                //copytokditem.set
                copytokditem.setCreatedate(date);
                copytokditem.setCopyTime(date);
                if (flagitem) {
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }


    /**
     * 采收入库
     *
     * @param wif
     * @param date
     */
    @Transactional
    public void harvestn(Whinform wif, Date date) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        copytokd.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
        copytokd.setSrguid(wif.getStorermguid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(wif.getQty());
        copytokd.setTotalamount(wif.getPrice());
        copytokd.setSupplierid(wif.getSupplierId());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();
        if (wif.getWhinformitemList() != null) {
            for (Whinformitem item : wif.getWhinformitemList()) {
                boolean flagitem = true;
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(copyId);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(item.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                copytokditem.setQty(item.getWeight());
                getQtyIn(copytokditem, item);
                copytokditem.setSaleorgid(wif.getSettlOrgId());
                BigDecimal amout = this.copytokdService.getSettlement(wif.getFoguid());
                copytokditem.setAmount(amout);
                if(copytokditem.getKdqty() != null){
                    copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(),6, RoundingMode.HALF_UP));
                }
                //copytokditem.setKdqty();//?????
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                copytokditem.setSupplierid(wif.getSupplierId());
                copytokditem.setBalancesupplierid(wif.getSupplierId());
                // copytokditem.setCurrency(item.get); //币种没有
                //copytokditem.set
                copytokditem.setCreatedate(date);
                copytokditem.setCopyTime(date);
                if (flagitem) {
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }


    /**
     * 生产加工入库
     *
     * @param wif
     * @param date
     */
    @Transactional
    public void productionIn(Whinform wif, Date date) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        copytokd.setKdtype(KDWhinformTypeEnums.PRODUCTION.getCode());
        copytokd.setSrguid(wif.getStorermguid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(wif.getQty());
        copytokd.setTotalamount(wif.getPrice());
        copytokd.setSupplierid(wif.getSupplierId());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();
        if (wif.getWhinformitemList() != null) {
            for (Whinformitem item : wif.getWhinformitemList()) {
                boolean flagitem = true;
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(copyId);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(item.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                copytokditem.setQty(item.getWeight());
                //copytokditem.setQty(item.getWeight());
                getQtyIn(copytokditem, item);
                copytokditem.setSaleorgid(wif.getSettlOrgId());
                copytokditem.setPrice(BigDecimal.ZERO);
                //copytokditem.setKdqty();//?????
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                copytokditem.setSupplierid(wif.getSupplierId());
                copytokditem.setBalancesupplierid(wif.getSupplierId());
                copytokditem.setDepartmentid(wif.getDepartmentid());
                // copytokditem.setCurrency(item.get); //币种没有
                //copytokditem.set
                copytokditem.setCreatedate(date);
                copytokditem.setCopyTime(date);
                if (flagitem) {
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }

    /**
     * 调整入库
     *
     * @param wif
     * @param date
     */
    @Transactional
    public void justingIn(Whinform wif, Date date) {
        getI(wif.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wif.getAutoid());
        copytokd.setLinkedcode(wif.getFormno());
        copytokd.setOrderguid(wif.getGuid());
        copytokd.setCode(wif.getBusinesscode());
        copytokd.setSrguid(wif.getStorermguid());
        copytokd.setCurrency(wif.getCurrency());
        copytokd.setKdcode(wif.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.IN.getCode());
        copytokd.setSubtype(wif.getWhintype());
        copytokd.setKdtype(KDWhinformTypeEnums.OTHER.getCode());
        copytokd.setSrguid(wif.getStorermguid());
        //copytokd.setSrname();  //表里没有库房名称
        //copytokd.setSrKdn();  //没有金蝶库房编码
        copytokd.setOrgguid(wif.getOrgguid());
        copytokd.setOrgname(wif.getOrgname());
        copytokd.setDepartmentid(wif.getDepartmentid());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(wif.getQty());
        copytokd.setTotalamount(wif.getPrice());
        //copytokd.setSupplierKdn(); //没有金蝶供应商编码
        //没有客户
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wif.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wif.getCreatedate());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();
        if (wif.getWhinformitemList() != null) {
            for (Whinformitem item : wif.getWhinformitemList()) {
                boolean flagitem = true;
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(copyId);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(item.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                copytokditem.setQty(item.getWeight());
                getQtyIn(copytokditem, item);
                copytokditem.setSaleorgid(wif.getSettlOrgId());
                copytokditem.setPrice(BigDecimal.ZERO);
                //copytokditem.setKdqty();//?????
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                // copytokditem.setCurrency(item.get); //币种没有
                //copytokditem.set
                copytokditem.setCreatedate(date);
                copytokditem.setCopyTime(date);
                if (flagitem) {
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }

    /**
     * 调拨出库单
     */

    public void redeployOut(Whoutform wof, Date date,BigDecimal qty,Integer type,Transferapplyitem t) {
        getI(wof.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(wof.getAutoid());
        copytokd.setLinkedcode(wof.getWhoutcode());
        copytokd.setOrderguid(wof.getGuid());
        copytokd.setCode(wof.getBusinesscode());
        copytokd.setKdcode(wof.getBusinesscode() + "A" + i);
        copytokd.setSrguid(wof.getSrguid());
        copytokd.setCurrency(wof.getCurrency());
        copytokd.setDepartmentid(wof.getDepartmentid());
        //copytokd.setKdcode();   //应该没用
        copytokd.setType(BillTypeEnums.OUT.getCode());
        copytokd.setSubtype(wof.getOuttype());
        if(type == 1){
            if (wof.getOrgguid().equals(wof.getSettlOrgId())) {
                //同组织调拨出库
                copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
            } else {
                //跨组织调拨出库
                copytokd.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
            }
        }else{
            copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        }

        copytokd.setOrgguid(wof.getOrgguid());
        copytokd.setOrgname(wof.getOrgname());
        //copytokd.setOrgKdn(); //没有金蝶组织编码
        copytokd.setTotalqty(qty);
        List<Customer> customers = customerList.stream().filter(c -> wof.getSettlOrgId().equals(c.getOrgguid())).collect(Collectors.toList());
        if (customers.size() == 1) {
            copytokd.setCustomerid(customers.get(0).getGuid());
        } else {
            flag = false;
            copytokd.setCopyRemark(copytokd.getCopyRemark() + "没有找到客户,");
        }
        if (flag) {
            copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokd.setStatus(CopyStatusEnums.CANT.getCode());
        }
        copytokd.setOperator(wof.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(wof.getCreatetime());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyId = copytokd.getId();

        boolean flagitem = true;
        Copytokditem copytokditem = new Copytokditem();
        copytokditem.setCopyRemark("");
        copytokditem.setCtkdid(copyId);
        copytokditem.setSrguid(t.getTransferapply().getFromsrguid());
        copytokditem.setShrguid(t.getFromshguid());
        copytokditem.setOrgguid(t.getTransferapply().getOrgid());
        copytokditem.setOrgname(t.getTransferapply().getOrgname());
        copytokditem.setProductguid(t.getProductguid());
        copytokditem.setSpguid(t.getSpguid());
        copytokditem.setQty(qty);
        //getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "3",null);
        if (wof.getOrgguid().equals(wof.getSettlOrgId())) {
            copytokditem.setPrice(BigDecimal.ZERO);
        }else{
            copytokditem.setPrice(t.getInterSettlPrice());
        }
        copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
        copytokditem.setUnit(t.getStockunit());
        copytokditem.setUnitid(t.getStockunitid());
        List<Customer> clistitem = customerList.stream().filter(c -> (wof.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
        if (clistitem.size() == 1) {
            copytokditem.setBalancecustomerid(clistitem.get(0).getGuid());
            copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());
            copytokditem.setPaymentcustomerid(clistitem.get(0).getGuid());
        } else {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到客户数据,");
            flagitem = false;
        }
        // copytokditem.setCurrency(item.get); //币种没有
        //copytokditem.set
        if (flagitem) {
            copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokditemService.insertCopytokditem(copytokditem);
//        if (wof.getWhoutformitemList() != null) {
//            for (Whoutformitem item : wof.getWhoutformitemList()) {
//                boolean flagitem = true;
//                Copytokditem copytokditem = new Copytokditem();
//                copytokditem.setCopyRemark("");
//                copytokditem.setCtkdid(copyId);
//                copytokditem.setSrguid(item.getSrguid());
//                copytokditem.setShrguid(item.getShrguid());
//                copytokditem.setOrgguid(item.getOrgguid());
//                copytokditem.setOrgname(item.getOrgname());
//                copytokditem.setProductguid(item.getProductguid());
//                copytokditem.setSpguid(item.getSpguid());
//                copytokditem.setQty(qty);
//                getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "3",null);
//                copytokditem.setPrice(t.getInterSettlPrice());
//                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
//                copytokditem.setUnit(item.getStockunit());
//                copytokditem.setUnitid(item.getStockunitid());
//                List<Customer> clistitem = customerList.stream().filter(c -> (wof.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
//                if (clistitem.size() == 1) {
//                    copytokditem.setBalancecustomerid(clistitem.get(0).getGuid());
//                    copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());
//                    copytokditem.setPaymentcustomerid(clistitem.get(0).getGuid());
//                } else {
//                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到客户数据,");
//                    flagitem = false;
//                }
//                // copytokditem.setCurrency(item.get); //币种没有
//                //copytokditem.set
//                if (flagitem) {
//                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
//                } else {
//                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
//                }
//                this.copytokditemService.insertCopytokditem(copytokditem);
//            }
//        }
    }


    /**
     * 调整出库
     *
     * @param whoutform
     * @param date
     */
    @Transactional
    public void justingOut(Whoutform whoutform, Date date) {
        getI(whoutform.getBusinesscode());
        boolean flag = true;
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(whoutform.getAutoid());
        copytokd.setOrderguid(whoutform.getGuid());
        copytokd.setCode(whoutform.getBusinesscode());
        copytokd.setLinkedcode(whoutform.getWhoutcode());
        copytokd.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.OUT.getCode());
        copytokd.setSubtype(whoutform.getOuttype());
        copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        copytokd.setSrguid(whoutform.getSrguid());
        copytokd.setCurrency(whoutform.getCurrency());
        copytokd.setDepartmentid(whoutform.getDepartmentid());
        //copytokd.setSrname();//库房没有
        //copytokd.setSrKdn();//库房金蝶编号没有
        copytokd.setOrgguid(whoutform.getOrgguid());
        copytokd.setOrgname(whoutform.getOrgname());
        //copytokd.setOrgKdn();//没有组织编码金蝶编号
        copytokd.setTotalqty(whoutform.getWeight());
        //copytokd.setTotalamount(whoutform.get);//没有总价
        copytokd.setOperator(whoutform.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(whoutform.getOuttime());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyid = copytokd.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCopyRemark("");
                copytokditem.setCtkdid(copyid);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(whoutform.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                copytokditem.setSpguid(item.getSpguid());
                getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "3",null);
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setSaleorgid(whoutform.getOrgguid());
                //copytokditem.setSaleorgKdn();//没有销售组织金蝶编号
                copytokditem.setOwner(whoutform.getOperatorid());
                //copytokditem.setOrdercustomerKdn();//没有
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                copytokditem.setPrice(BigDecimal.ZERO);

                copytokditem.setCopyTime(date);
                copytokditem.setLastCopyTime(date);
                copytokditem.setCreatedate(date);
                copytokditem.setLastupdatetime(date);

                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }

    /**
     * 报废出库
     *
     * @param whoutform
     * @param date
     */
    @Transactional
    public void scrappingOut(Whoutform whoutform, Date date) {
        getI(whoutform.getBusinesscode());
        Copytokd copytokd = new Copytokd();
        copytokd.setCopyRemark("");
        copytokd.setLinkedautoid(whoutform.getAutoid());
        copytokd.setOrderguid(whoutform.getGuid());
        copytokd.setCode(whoutform.getBusinesscode());
        copytokd.setLinkedcode(whoutform.getWhoutcode());
        copytokd.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokd.setType(BillTypeEnums.OUT.getCode());
        copytokd.setSubtype(whoutform.getOuttype());
        copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        copytokd.setSrguid(whoutform.getSrguid());
        copytokd.setCurrency(whoutform.getCurrency());
        copytokd.setDepartmentid(whoutform.getDepartmentid());
        //copytokd.setSrname();//库房没有
        //copytokd.setSrKdn();//库房金蝶编号没有
        copytokd.setOrgguid(whoutform.getOrgguid());
        copytokd.setOrgname(whoutform.getOrgname());
        //copytokd.setOrgKdn();//没有组织编码金蝶编号
        copytokd.setTotalqty(whoutform.getWeight());
        //copytokd.setTotalamount(whoutform.get);//没有总价
        copytokd.setOperator(whoutform.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokd.setOrderdate(whoutform.getOuttime());
        copytokd.setCopyTime(date);
        copytokd.setLastCopyTime(date);
        copytokd.setCreatetime(date);
        copytokd.setIssplit(IssplitEnums.NO.getCode());
        copytokd.setStatus(CopyStatusEnums.INIT.getCode());
        this.copytokdService.insertCopytokd(copytokd);
        Long copyid = copytokd.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCopyRemark("");
                copytokditem.setCtkdid(copyid);
                copytokditem.setSrguid(item.getSrguid());
                copytokditem.setShrguid(item.getShrguid());
                copytokditem.setOrgguid(whoutform.getOrgguid());
                copytokditem.setOrgname(item.getOrgname());
                copytokditem.setProductguid(item.getProductguid());
                if (StringUtils.isNotEmpty(item.getSpguid())) {
                    copytokditem.setSpguid(item.getSpguid());
                } else {
                    copytokditem.setSpguid(item.getRawspguid());
                }
                getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "3",null);
                copytokditem.setSaleorgid(whoutform.getOrgguid());
                copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                copytokditem.setOwner(whoutform.getOperatorid());
                copytokditem.setUnit(item.getStockunit());
                copytokditem.setUnitid(item.getStockunitid());
                copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                copytokditem.setPrice(BigDecimal.ZERO);

                copytokditem.setCopyTime(date);
                copytokditem.setLastCopyTime(date);
                copytokditem.setCreatedate(date);
                copytokditem.setLastupdatetime(date);

                this.copytokditemService.insertCopytokditem(copytokditem);
            }
        }
    }


    /**
     * 生产领料出库
     * 从生产报表中获取数据
     *
     * @param startDate
     * @param copyparam
     */
    @Transactional
    public void productionOut(Date startDate, Copyparam copyparam) {
        Date date = DateUtils.getNowDate();

        Copytokd copytokdS = new Copytokd();
        copytokdS.setType(BillTypeEnums.OUT.getCode());
        copytokdS.setSubtype(WhoutformTypeEnums.PRODUCTION.getCode());
        Long autoid = this.copytokdService.getMaxlinkedid(copytokdS);
        Producereport producereport = new Producereport();
        if (StringUtils.isNotEmpty(copyparam.getConditions())) {
            producereport.setConditions(copyparam.getConditions());
        }
        if (autoid > 0) {
            producereport.setId(autoid);
        } else {
            producereport.setBeginCreatedate(startDate);
            producereport.setEndCreatedate(DateUtils.getDateBeforeMinite(date, -3));
        }
        List<Producereport> producereportList = this.producereportService.selectProducereportListAll(producereport);
        for (Producereport p : producereportList) {
            getProductionI(p);
            if (p.getIntype() == 2) {
                //辅料
                Copytokd copytokd = new Copytokd();
                copytokd.setCopyRemark("");
                copytokd.setLinkedautoid(p.getId());
                copytokd.setOrderguid(p.getPlanguid());
                copytokd.setCode(p.getPlcode());
                //copytokd.setLinkedcode();
                copytokd.setKdcode(p.getPlcode() + "A" + i);
                copytokd.setType(BillTypeEnums.OUT.getCode());
                copytokd.setSubtype(WhoutformTypeEnums.PRODUCTION.getCode());
                copytokd.setKdtype(KDWhoutformTypeEnums.PRODUCTION.getCode());
                copytokd.setSrguid(p.getSrguid());
                //copytokd.setCurrency(whoutform.getCurrency());
                copytokd.setDepartmentid(p.getDepartid());
                //copytokd.setSrname();//库房没有
                //copytokd.setSrKdn();//库房金蝶编号没有
                copytokd.setOrgguid(p.getOrgguid());
                copytokd.setOrgname(p.getOrgname());
                //copytokd.setOrgKdn();//没有组织编码金蝶编号
                copytokd.setTotalqty(p.getOutweight());
                //copytokd.setTotalamount(whoutform.get);//没有总价
                copytokd.setOperator(p.getOperatorid());
                //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
                copytokd.setOrderdate(p.getOrderdate());
                copytokd.setCopyTime(date);
                copytokd.setLastCopyTime(date);
                copytokd.setCreatetime(date);
                copytokd.setIssplit(IssplitEnums.NO.getCode());
                copytokd.setStatus(CopyStatusEnums.INIT.getCode());
                this.copytokdService.insertCopytokd(copytokd);
                Long copyid = copytokd.getId();
                if (p.getProducereportitemList() != null) {
                    for (int z = 0; z < p.getProducereportitemList().size(); z++) {
                        Producereportitem item = p.getProducereportitemList().get(z);
                        Copytokditem copytokditem = new Copytokditem();
                        copytokditem.setCopyRemark("");
                        copytokditem.setCtkdid(copyid);
                        copytokditem.setSrguid(p.getSrguid());
                        copytokditem.setShrguid(item.getShrguid());
                        copytokditem.setOrgguid(p.getOrgguid());
                        copytokditem.setOrgname(p.getOrgname());
                        copytokditem.setProductguid(item.getOutproductid());
                        copytokditem.setSpguid(item.getOutspecid());
                        copytokditem.setDepartmentid(p.getDepartid());
                        copytokditem.setOwner(p.getOperatorid());
                        copytokditem.setQty(item.getOutweight());
                        //copytokditem.setOrdercustomerKdn();//没有
                        copytokditem.setUnit(item.getOutstockunit());
                        copytokditem.setUnitid(item.getOutstockunitid());
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                        copytokditem.setPrice(BigDecimal.ZERO);
                        copytokditem.setCosguid(item.getCospguid());

                        copytokditem.setCopyTime(date);
                        copytokditem.setLastCopyTime(date);
                        copytokditem.setCreatedate(date);
                        copytokditem.setLastupdatetime(date);

                        this.copytokditemService.insertCopytokditem(copytokditem);
                    }
                }
            } else {
                //成品
                Copytokd copytokd = new Copytokd();
                copytokd.setCopyRemark("");
                copytokd.setLinkedautoid(p.getId());
                copytokd.setOrderguid(p.getPlanguid());
                copytokd.setCode(p.getPlcode());
                //copytokd.setLinkedcode();
                copytokd.setKdcode(p.getPlcode() + "A" + i);
                copytokd.setType(BillTypeEnums.OUT.getCode());
                copytokd.setSubtype(WhoutformTypeEnums.PRODUCTION.getCode());
                copytokd.setKdtype(KDWhoutformTypeEnums.PRODUCTION.getCode());
                copytokd.setSrguid(p.getSrguid());
                //copytokd.setCurrency(whoutform.getCurrency());
                copytokd.setDepartmentid(p.getDepartid());
                //copytokd.setSrname();//库房没有
                //copytokd.setSrKdn();//库房金蝶编号没有
                copytokd.setOrgguid(p.getOrgguid());
                copytokd.setOrgname(p.getOrgname());
                //copytokd.setOrgKdn();//没有组织编码金蝶编号
                copytokd.setTotalqty(p.getOutweight());
                //copytokd.setTotalamount(whoutform.get);//没有总价
                copytokd.setOperator(p.getOperatorid());
                //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
                copytokd.setOrderdate(p.getOrderdate());
                copytokd.setCopyTime(date);
                copytokd.setLastCopyTime(date);
                copytokd.setCreatetime(date);
                copytokd.setIssplit(IssplitEnums.NO.getCode());
                copytokd.setStatus(CopyStatusEnums.INIT.getCode());
                this.copytokdService.insertCopytokd(copytokd);
                Long copyid = copytokd.getId();
                if (p.getProducereportitemList() != null) {//处理明细数据
//                    BigDecimal total = BigDecimal.ZERO;
                    for (int z = 0; z < p.getProducereportitemList().size(); z++) {
//                        if (z < p.getProducereportitemList().size() - 1) {
                        Producereportitem item = p.getProducereportitemList().get(z);
                        Copytokditem copytokditem = new Copytokditem();
                        copytokditem.setCopyRemark("");
                        copytokditem.setCtkdid(copyid);
                        copytokditem.setSrguid(p.getSrguid());
                        copytokditem.setShrguid(item.getShrguid());
                        copytokditem.setOrgguid(p.getOrgguid());
                        copytokditem.setOrgname(p.getOrgname());
                        copytokditem.setProductguid(item.getOutproductid());
                        copytokditem.setSpguid(item.getOutspecid());
                        copytokditem.setDepartmentid(p.getDepartid());
                        copytokditem.setOwner(p.getOperatorid());
                        //明细出库重量=((领料出库总重量-总的退料重量)*明细入库重量))/总的入库重量
//                            copytokditem.setQty(((p.getOutweight().subtract(p.getBackweight())).multiply(item.getInweight())).divide(p.getInweight(), 2, BigDecimal.ROUND_HALF_DOWN));
                        copytokditem.setQty(item.getOutweight());
                        //copytokditem.setOrdercustomerKdn();//没有
                        copytokditem.setUnit(item.getInstockunit());
                        copytokditem.setUnitid(item.getInstockunitid());
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                        copytokditem.setPrice(BigDecimal.ZERO);
                        copytokditem.setCosguid(item.getCospguid());

                        copytokditem.setCopyTime(date);
                        copytokditem.setLastCopyTime(date);
                        copytokditem.setCreatedate(date);
                        copytokditem.setLastupdatetime(date);
                        //明细出库重量=((领料出库总重量-总的退料重量)*明细入库重量))/总的入库重量
//                            total = total.add(((p.getOutweight().subtract(p.getBackweight())).multiply(item.getInweight())).divide(p.getInweight(), 2, BigDecimal.ROUND_HALF_DOWN));

                        this.copytokditemService.insertCopytokditem(copytokditem);
//                        }
//                        else {//处理最后一条明细数据
//                            Producereportitem item = p.getProducereportitemList().get(z);
//                            Copytokditem copytokditem = new Copytokditem();
//                            copytokditem.setCopyRemark("");
//                            copytokditem.setCtkdid(copyid);
//                            copytokditem.setSrguid(p.getSrguid());
//                            copytokditem.setShrguid(item.getShrguid());
//                            copytokditem.setOrgguid(p.getOrgguid());
//                            copytokditem.setOrgname(p.getOrgname());
//                            copytokditem.setProductguid(item.getOutproductid());
//                            copytokditem.setSpguid(item.getOutspecid());
//                            copytokditem.setDepartmentid(p.getDepartid());
//                            copytokditem.setOwner(p.getOperatorid());
//                            //最后一条明细出库重量=领料出库总重量-总的退料重量-其他明细的出库总重量
//                            copytokditem.setQty((p.getOutweight().subtract(p.getBackweight())).subtract(total));
//                            //copytokditem.setOrdercustomerKdn();//没有
//                            copytokditem.setUnit(item.getInstockunit());
//                            copytokditem.setUnitid(item.getInstockunitid());
//                            copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
//                            copytokditem.setPrice(BigDecimal.ZERO);
//                            copytokditem.setCosguid(item.getCospguid());
//
//                            copytokditem.setCopyTime(date);
//                            copytokditem.setLastCopyTime(date);
//                            copytokditem.setCreatedate(date);
//                            copytokditem.setLastupdatetime(date);
//
//                            this.copytokditemService.insertCopytokditem(copytokditem);
//                        }

                    }
                }
            }
            this.copytokdService.updatePickinglist(p.getPlcode());

        }
    }



    public void saleOut(Whoutform whoutform, Date date) {
        Organization organization = this.organizationService.selectOrganizationById(whoutform.getSettlOrgId());
        Sellform sellform = this.sellformService.selectSellformByGuid(whoutform.getOrderguid());
        MultipartConfig multipartConfig = this.multipartConfigService.selectMultipartConfigBySellPayType(sellform.getPaytype());
        //出库组织和销售组织是否一样
        //一样折不拆分
        //不一样折拆分
        if (whoutform.getOrgguid().equals(whoutform.getSettlOrgId())) {
            addCopytodk(whoutform, sellform, date, multipartConfig, 0);
        } else {
            if (multipartConfig != null) {
                if (multipartConfig.getMultipartFlag().equals(MultipartEnums.NO.getCode())) {
                    //不需要内部结算
                    if ((!whoutform.getOrgguid().equals(whoutform.getSettlOrgId()))) {
                        this.addCopytodk(whoutform, sellform, date, multipartConfig, 1);
                    } else {
                        addCopytodk(whoutform, sellform, date, multipartConfig, 0);
                    }
                } else {
                    //需要拆分
                    //写入原单据
                    //销售组织是参数组织、拆分工厂到组织出入库单，组织到客户
                    //销售组织不是参数组织，拆分
                    if ((multipartConfig.getMultipartOrgguid().equals(whoutform.getOrgguid()))) {
                        //出库组织为集团
                        if (!whoutform.getOrgguid().equals(whoutform.getSettlOrgId())) {
                            this.addCopytodk(whoutform, sellform, date, multipartConfig, 1);
                        } else {
                            this.addCopytodk(whoutform, sellform, date, multipartConfig, 0);
                        }
                    } else {
                        if (multipartConfig.getMultipartOrgguid().equals(whoutform.getSettlOrgId())) {
                            this.addCopytodk(whoutform, sellform, date, multipartConfig, 1);
                        } else {
                            //销售组织不是参数组织，拆分工厂到参数组织，参数组织到销售组织   销售组织到客户
                            this.addCopytodk(whoutform, sellform, date, multipartConfig, 2);
                        }
                    }

                }
            } else {
                //没有查询到是否需要拆分
                addCopytodk(whoutform, sellform, date, multipartConfig, 0);
            }
        }

    }

    /**
     * 获取递增金蝶编号
     *
     * @param businesscode
     * @return
     */
    public int getI(String businesscode) {
        i = this.copytokdService.getSort(businesscode);
        return i;
    }

    /**
     * 获取领料出库递增便阿红
     *
     * @param producereport
     */
    public void getProductionI(Producereport producereport) {
        i = this.copytokdService.getSort(producereport.getPlcode());
    }


    /**
     * 入库单获取数量和金蝶数量
     *
     * 1、根据入库单明规格找物料
     * 2、查看物料金蝶单位
     * 3、查看物料库存单位
     * 4、看库存单位是否重量单位  是重量单位则转换，并写入金蝶数量  不是重量单位则将weight写入金蝶数量
     *
     *
     *
     */
    public void getQtyIn(Copytokditem copytokditem, Whinformitem whinformitem) {
            BigDecimal result = BigDecimal.ZERO;
            List<Salesspecification> salesspecifications = new ArrayList<Salesspecification>();
            if (StringUtils.isNotEmpty(whinformitem.getSpguid())) {
                salesspecifications = salesspecificationList.stream().filter(s -> whinformitem.getSpguid().equals(s.getGuid())).collect(Collectors.toList());
            } else {
                salesspecifications = salesspecificationList.stream().filter(s -> whinformitem.getRawspguid().equals(s.getGuid())).collect(Collectors.toList());
            }
            if (salesspecifications.size() == 1) {
                List<Salesspecification> finalSalesspecifications = salesspecifications;
                if (finalSalesspecifications.get(0).getKingdeeunitid() != null) {
                    List<Stockunit> stockunits = this.stockunitList.stream().filter(t -> finalSalesspecifications.get(0).getKingdeeunitid().equals(t.getId())).collect(Collectors.toList());
                    if (stockunits.size() == 1) {
                        if (stockunits.get(0).getIsUnit() == IsWeifhtUnitEnums.YES.getCode()) {
                            //是重量单位
                            List<Stockunit> stockunitsweight = this.stockunitList.stream().filter(tw -> whinformitem.getStockunitid().equals(tw.getId())).collect(Collectors.toList());
                            if (stockunitsweight.size() == 1) {
                                copytokditem.setKdqty(whinformitem.getWeight().multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT()));
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有库存单位,");
                                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        } else {
                            copytokditem.setKdqty(whinformitem.getWeight());
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有金蝶单位编码,");
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }
                } else {
                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有金蝶单位编码,");
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                }
            } else {
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料错误,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            }
    }



    /**
     * 出库单获取数量
     *
     * @param copytokditem
     * @param whoutformitem
     * @param type          IsPresentKDEnums   是否总送
     * @param isSplit       1拆分的数据   2拆分后最后一条   3其他数据
     *
     *
     *
     */
    public void getQty(Copytokditem copytokditem, Whoutformitem whoutformitem, String type, String isSplit, Sellform sellform) {
        if(copytokditem.getQty() == null){
            BigDecimal result = BigDecimal.ZERO;
            List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s -> copytokditem.getSpguid().equals(s.getGuid())).collect(Collectors.toList());
            if (salesspecifications.size() == 1) {
                if (salesspecifications.get(0).getKingdeeunitid() != null) {
                    List<Stockunit> stockunits = this.stockunitList.stream().filter(t -> salesspecifications.get(0).getKingdeeunitid().equals(t.getId())).collect(Collectors.toList());
                    if (stockunits.size() == 1) {
                        if (stockunits.get(0).getIsUnit() == IsWeifhtUnitEnums.YES.getCode()) {
                            //是重量单位
                            List<Stockunit> stockunitsweight = this.stockunitList.stream().filter(tw -> whoutformitem.getStockunitid().equals(tw.getId())).collect(Collectors.toList());
                            if ("1".equals(isSplit)) {
                                copytokditem.setQty(whoutformitem.getWeight());
                                if (stockunitsweight.size() == 1) {
                                    copytokditem.setKdqty(whoutformitem.getWeight().multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT()));
                                } else {
                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有库存单位,");
                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                                if (whoutformitem.getLoadcount() != null) {
                                    copytokditem.setLoadcount(new BigDecimal(whoutformitem.getLoadcount()));
                                }


                                if(copytokditem.getPrice() == null){
                                    if (salesspecifications.get(0).getBrand().getFlag() == 1) {
                                        if(sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)){
                                            //赠送，样品，促销  非标品按参数中的内部价格
                                            if(innPrice == null){
                                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到单价,");
                                                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                            }else{
                                                copytokditem.setPrice(innPrice);
                                                copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                            }
                                        }else{
                                            List<Sellitem> sellitems = sellform.getSellitemList().stream().filter(i -> i.getGuid().equals(whoutformitem.getItemguid())).collect(Collectors.toList());
                                            //Sellitem sellitem = this.sellitemService.selectSellitemById(whoutformitem.getItemguid());
                                            if (sellitems.size() == 1) {
                                                List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(sellitems.get(0).getSfunitid())).collect(Collectors.toList());
                                                if (sellunit.size() == 1) {
                                                    if(sellform.getIsdiscount() == 1){
                                                        //折扣订单
                                                        if(innDiscount == null){
                                                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到折扣,");
                                                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                        }else{
                                                            copytokditem.setPrice((whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()).multiply(BigDecimal.ONE.subtract(innDiscount))));
                                                            copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                        }
                                                    }else{
                                                        copytokditem.setPrice((whoutformitem.getPrice().multiply(sellunit.get(0).getToKg())));
                                                        copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                    }
                                                } else {
                                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                }
                                            } else {
                                                List<Sellitem> itemList = sellform.getSellitemList().stream().filter(i -> i.getSpguid().equals(whoutformitem.getSpguid())).collect(Collectors.toList());
                                                if(itemList.size() > 0){
                                                    List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(itemList.get(0).getSfunitid())).collect(Collectors.toList());
                                                    if (sellunit.size() == 1) {
                                                        if(sellform.getIsdiscount() == 1){
                                                            //折扣订单
                                                            if(innDiscount == null){
                                                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到折扣,");
                                                                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                            }else{
                                                                copytokditem.setPrice((whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()).multiply(BigDecimal.ONE.subtract(innDiscount))));
                                                                copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                            }
                                                        }else{
                                                            copytokditem.setPrice((whoutformitem.getPrice().multiply(sellunit.get(0).getToKg())));
                                                            copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                        }
                                                    } else {
                                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                    }
                                                }else{
                                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到销售明细,");
                                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                }
                                            }
                                        }
                                    }
                                }
                            } else if ("2".equals(isSplit)) {
                                if (type.equals(IsPresentKDEnums.FALSE.getCode())) {
                                    //不是赠送
                                    if (stockunitsweight.size() == 1) {
                                        if (whoutformitem.getHandselweight() != null) {
                                            result = (whoutformitem.getWeight().subtract(whoutformitem.getHandselweight())).multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT());
                                            copytokditem.setQty(whoutformitem.getWeight().subtract(whoutformitem.getHandselweight()));
                                        } else {
                                            result = (whoutformitem.getWeight()).multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT());
                                            copytokditem.setQty(whoutformitem.getWeight());
                                        }
                                        copytokditem.setKdqty(result);
                                        if (whoutformitem.getLoadcount() != null) {
                                            if (whoutformitem.getHandsel() != null) {
                                                copytokditem.setLoadcount(new BigDecimal(whoutformitem.getLoadcount()).subtract(whoutformitem.getHandsel()));
                                            } else {
                                                copytokditem.setLoadcount(new BigDecimal(whoutformitem.getLoadcount()));
                                            }
                                        }
                                    } else {
                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有库存单位,");
                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                    if(copytokditem.getPrice() == null){
                                        if (whoutformitem.getLoadcount() != null) {
                                            if (whoutformitem.getLoadcount() != 0) {
                                                copytokditem.setAmount(whoutformitem.getPrice().multiply(copytokditem.getLoadcount()));
                                                copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(), 6));
                                            } else {
                                                //出库单的itemguid找销售单明细
                                                List<Sellitem> sellitems = sellform.getSellitemList().stream().filter(i -> i.getGuid().equals(whoutformitem.getItemguid())).collect(Collectors.toList());
                                                if (sellitems.size() == 1) {
                                                    List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(sellitems.get(0).getSfunitid())).collect(Collectors.toList());
                                                    if (sellunit.size() == 1) {
                                                        copytokditem.setPrice(whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()));
                                                        copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                    } else {
                                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                    }

                                                } else {
                                                    //出库单的物料id找销售单明细
                                                    List<Sellitem> itemList = sellform.getSellitemList().stream().filter(i -> i.getSpguid().equals(whoutformitem.getSpguid())).collect(Collectors.toList());
                                                    if(itemList.size() > 0){
                                                        List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(itemList.get(0).getSfunitid())).collect(Collectors.toList());
                                                        if (sellunit.size() == 1) {
                                                            copytokditem.setPrice(whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()));
                                                            copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                        } else {
                                                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                        }
                                                    }else{
                                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到销售明细,");
                                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                    }
                                                }
                                            }
                                        } else {
                                            Sellitem sellitem = this.sellitemService.selectSellitemById(whoutformitem.getItemguid());
                                            if (sellitem != null) {
                                                List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(sellitem.getSfunitid())).collect(Collectors.toList());
                                                if (sellunit.size() == 1) {
                                                    copytokditem.setPrice(whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()));
                                                    copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                } else {
                                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                }

                                            } else {
                                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到销售明细,");
                                                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                            }
                                        }
                                    }


                                } else {
                                    //是赠送
                                    if (stockunitsweight.size() == 1) {
                                        result = whoutformitem.getHandselweight().multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT());
                                        copytokditem.setQty(whoutformitem.getHandselweight());
                                        copytokditem.setKdqty(result);
                                        if (whoutformitem.getHandsel() != null) {
                                            copytokditem.setLoadcount(whoutformitem.getHandsel());
                                        }
                                    } else {
                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有库存单位,");
                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                    if(copytokditem.getPrice() == null){
                                        if (whoutformitem.getLoadcount() != null) {
                                            if (whoutformitem.getLoadcount() != 0) {
                                                copytokditem.setAmount(whoutformitem.getPrice().multiply(copytokditem.getLoadcount()));
                                                copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(), 6));

                                            } else {
                                                Sellitem sellitem = this.sellitemService.selectSellitemById(whoutformitem.getItemguid());
                                                if (sellitem != null) {
                                                    List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(sellitem.getSfunitid())).collect(Collectors.toList());
                                                    if (sellunit.size() == 1) {
                                                        copytokditem.setPrice(whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()));
                                                        copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                    } else {
                                                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                    }
                                                } else {
                                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到销售明细,");
                                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                }
                                            }
                                        } else {
                                            Sellitem sellitem = this.sellitemService.selectSellitemById(whoutformitem.getItemguid());
                                            if (sellitem != null) {
                                                List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> s.getId().equals(sellitem.getSfunitid())).collect(Collectors.toList());
                                                if (sellunit.size() == 1) {
                                                    copytokditem.setPrice(whoutformitem.getPrice().multiply(sellunit.get(0).getToKg()));
                                                    copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                                                } else {
                                                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "销售明细没有找到单位,");
                                                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                                }

                                            } else {
                                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到销售明细,");
                                                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                                            }
                                        }
                                    }
                                }
                            } else {
                                copytokditem.setQty(whoutformitem.getWeight());
                                copytokditem.setKdqty(whoutformitem.getWeight().multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT()));
                            }
                        } else {
                            //不是重量单位
                            if ("1".equals(isSplit)) {
                                copytokditem.setQty(new BigDecimal(whoutformitem.getLoadcount()));
                                copytokditem.setKdqty(new BigDecimal(whoutformitem.getLoadcount()));
                                if (whoutformitem.getLoadcount() != null) {
                                    copytokditem.setLoadcount(new BigDecimal(whoutformitem.getLoadcount()));
                                }
                            } else if ("2".equals(isSplit)) {
                                if (type.equals(IsPresentKDEnums.FALSE.getCode())) {
                                    //不是赠送
                                    if (whoutformitem.getHandsel() != null) {
                                        result = new BigDecimal(whoutformitem.getLoadcount()).subtract(whoutformitem.getHandsel());
                                        copytokditem.setLoadcount(new BigDecimal(whoutformitem.getLoadcount()));
                                    } else {
                                        result = BigDecimal.ZERO;
                                        copytokditem.setLoadcount(BigDecimal.ZERO);
                                    }
                                    copytokditem.setQty(result);
                                    copytokditem.setKdqty(result);
                                    copytokditem.setAmount(result.multiply(whoutformitem.getPrice()));
                                    copytokditem.setPrice(copytokditem.getAmount().divide(result, 6));
                                } else {
                                    //是赠送
                                    copytokditem.setQty(whoutformitem.getHandsel());
                                    copytokditem.setKdqty(whoutformitem.getHandsel());
                                    copytokditem.setAmount(whoutformitem.getHandsel().multiply(whoutformitem.getPrice()));
                                    copytokditem.setPrice(copytokditem.getAmount().divide(whoutformitem.getHandsel(), 6));
                                }
                            } else {
                                copytokditem.setQty(whoutformitem.getWeight());
                                copytokditem.setKdqty(whoutformitem.getWeight());
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有金蝶单位编码,");
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }
                } else {
                    if ("3".equals(isSplit)) {
                        List<Stockunit> stockunitsweight = this.stockunitList.stream().filter(tw -> whoutformitem.getStockunitid().equals(tw.getId())).collect(Collectors.toList());
                        if (stockunitsweight.size() == 1) {
                            copytokditem.setQty(whoutformitem.getWeight());
//                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有金蝶单位编码,");
//                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                        } else {
                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到物料库存单位,");
                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料错误,");
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                }
            } else {
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有金蝶单位编码,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            }
        }


    }


    /**
     * 销售出库单第一条数据
     *
     * @param whoutform
     * @param sellform
     * @param date
     * @param multipartConfig
     * @param type   0只有一条数据，需要上传金蝶   1该方法为原始单据，拆分3条     2该方法为原始单据，状态是已拆分，拆分5条数据
     */
    @Transactional
    public void addCopytodk(Whoutform whoutform, Sellform sellform, Date date, MultipartConfig multipartConfig, Integer type) {


            if(whoutform.getBusinesscode().equals("SF210535")){
                System.out.println("123");
            }
            getI(whoutform.getBusinesscode());
            boolean flag = true;
            Copytokd copytokd = new Copytokd();
            copytokd.setCopyRemark("");
            copytokd.setLinkedautoid(whoutform.getAutoid());
            copytokd.setOrderguid(whoutform.getGuid());
            copytokd.setCode(sellform.getSellcode());
            copytokd.setLinkedcode(whoutform.getWhoutcode());
            copytokd.setKdcode(whoutform.getBusinesscode() + "A" + i);
            copytokd.setDepartmentid(whoutform.getDepartmentid());
            copytokd.setKdsellpaytype(multipartConfig.getKdsellpaytype());
            copytokd.setType(BillTypeEnums.OUT.getCode());
            copytokd.setSubtype(WhoutformTypeEnums.SALE.getCode());
            copytokd.setSellpaytype(sellform.getPaytype());
            if (type == 0) {
                //如果为样品，赠送，则送其他出库单
                if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)) {
                    copytokd.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
                } else {
                    copytokd.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
                }
            } else {
                copytokd.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
            }
            //copytokd.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
            copytokd.setSrguid(whoutform.getSrguid());
            copytokd.setCurrency(whoutform.getCurrency());
            //copytokd.setSrname();//库房没有
            //copytokd.setSrKdn();//库房金蝶编号没有
            copytokd.setOrgguid(whoutform.getOrgguid());
            copytokd.setOrgname(whoutform.getOrgname());
            //copytokd.setOrgKdn();//没有组织编码金蝶编号
            copytokd.setTotalqty(whoutform.getWeight());
            //copytokd.setTotalamount(whoutform.get);//没有总价
            copytokd.setOperator(whoutform.getOperatorid());
            //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
            copytokd.setOrderdate(whoutform.getOuttime());
            copytokd.setCopyTime(date);
            copytokd.setLastCopyTime(date);
            copytokd.setCreatetime(date);
            copytokd.setIssplit(IssplitEnums.NO.getCode());
            if (type != 0) {
                copytokd.setStatus(CopyStatusEnums.SPLIT.getCode());
                copytokd.setCustomerid(whoutform.getRevCustId());
            } else {
                if (!sellform.getPaytype().equals(5)) {
                    copytokd.setCustomerid(whoutform.getRevCustId());
                }
                copytokd.setStatus(CopyStatusEnums.INIT.getCode());
            }


            this.copytokdService.insertCopytokd(copytokd);
            Long copyid = copytokd.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(copyid);
                    copytokditem.setSrguid(item.getSrguid());
                    copytokditem.setShrguid(item.getShrguid());
                    copytokditem.setOrgguid(whoutform.getOrgguid());
                    copytokditem.setOrgname(item.getOrgname());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());
//                if (item.getHandsel() != null) {
//                    copytokditem.setQty(item.getWeight().subtract(item.getHandsel()));
//                } else {
//                    copytokditem.setQty(item.getWeight());
//                }
                    //copytokditem.setKdqty();//?????

                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "2",sellform);
                    //copytokditem.setQty(item.getWeight());
                    copytokditem.setSaleorgid(whoutform.getOrgguid());
                    //copytokditem.setSaleorgKdn();//没有销售组织金蝶编号
                    copytokditem.setOwner(whoutform.getOperatorid());
                    //copytokditem.setBalancesupplierid(whoutform.gets); //结算供应商id

                    //copytokditem.setOrdercustomerKdn();//没有
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    //copytokditem.setPrice(item.getPrice());

//                List<Supplier> suppliersitem = supplierList.stream().filter( s -> whoutform.getOrgguid().equals(s.getOrgguid())).collect(Collectors.toList());
//                if(suppliersitem.size() == 1){
//                    copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
//                    copytokditem.setSupplierid(suppliersitem.get(0).getId());
//                }else{
//                    copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到供应商数据");
//                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
//                }
                    if (type == 0) {
                        if (!sellform.getPaytype().equals(4) && !sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(6)) {
                            copytokditem.setBalancecustomerid(whoutform.getRevCustId());
                            copytokditem.setOrdercustomerid(whoutform.getRevCustId());
                            copytokditem.setPaymentcustomerid(whoutform.getRevCustId());
                            copytokditem.setOrdercustomerid(whoutform.getRevCustId());
                        }
                    }
                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    this.copytokditemService.insertCopytokditem(copytokditem);

                    if (item.getHandsel() != null) {
                        if (item.getHandsel().compareTo(BigDecimal.ZERO) != 0) {
                            Copytokditem copytokditem1 = new Copytokditem();
                            copytokditem1.setCtkdid(copyid);
                            copytokditem1.setSrguid(item.getSrguid());
                            copytokditem1.setShrguid(item.getShrguid());
                            copytokditem1.setOrgguid(item.getOrgguid());
                            copytokditem1.setOrgname(item.getOrgname());
                            copytokditem1.setProductguid(item.getProductguid());
                            copytokditem1.setSpguid(item.getSpguid());
                            //copytokditem1.setQty(item.getHandsel());
                            //copytokditem.setKdqty();//?????
                            getQty(copytokditem1, item, IsPresentKDEnums.TRUE.getCode(), "2",sellform);
                            copytokditem1.setIspresent(IsPresentKDEnums.TRUE.getCode());
                            copytokditem1.setSaleorgid(whoutform.getOrgguid());
                            //copytokditem.setSaleorgKdn();//没有销售组织金蝶编号
                            copytokditem1.setOwner(whoutform.getOperatorid());
                            //copytokditem.setBalancesupplierid(whoutform.gets); //结算供应商id
                            //copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                            //copytokditem.setOrdercustomerKdn();//没有
                            copytokditem1.setStatus(CopyStatusEnums.INIT.getCode());
                           // copytokditem1.setPrice(item.getPrice());
                            copytokditem1.setUnit(item.getStockunit());
                            copytokditem1.setUnitid(item.getStockunitid());
//                        List<Supplier> suppliersitem1 = supplierList.stream().filter( s -> whoutform.getGuid().equals(s.getOrgguid())).collect(Collectors.toList());
//                        if(suppliersitem1.size() == 1){
//                            copytokditem.setBalancesupplierid(suppliersitem1.get(0).getId());
//                            copytokditem.setSupplierid(suppliersitem1.get(0).getId());
//                        }else{
//                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到供应商数据");
//                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
//                        }
                            if (type == 0) {
                                if (!sellform.getPaytype().equals(4) && !sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(6)) {
                                    copytokditem1.setBalancecustomerid(whoutform.getRevCustId());
                                    copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                                    copytokditem1.setPaymentcustomerid(whoutform.getRevCustId());
                                    copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                                }
                            }

                            copytokditem1.setCopyTime(date);
                            copytokditem1.setLastCopyTime(date);
                            copytokditem1.setCreatedate(date);
                            copytokditem1.setLastupdatetime(date);

                            copytokditem1.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());

//                        if (item.getLoadcount() != null) {
//                            copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
//                        } else {
//                            copytokditem.setLoadcount(BigDecimal.ZERO);
//                        }


                            this.copytokditemService.insertCopytokditem(copytokditem1);
                        }
                    }
                }


            }

        }
        if (type == 1) {
            //销售组织即集团组织
            //工厂到销售，销售到客户
            chai(whoutform, sellform, date, multipartConfig, copytokd.getId());

        } else if (type == 2) {
            chaiAll(whoutform, sellform, date, multipartConfig, copytokd.getId());
        }
    }

    public void chai(Whoutform whoutform, Sellform sellform, Date date, MultipartConfig multipartConfig, Long parentid) {
        //工厂到集团出库
        getI(whoutform.getBusinesscode());
        boolean flag1 = true;
        Copytokd copytokdout = new Copytokd();
        copytokdout.setCopyRemark("");
        copytokdout.setLinkedautoid(whoutform.getAutoid());
        copytokdout.setOrderguid(whoutform.getGuid());
        copytokdout.setCode(sellform.getSellcode());
        copytokdout.setLinkedcode(whoutform.getWhoutcode());
        copytokdout.setKdcode(whoutform.getBusinesscode() + "A" + i);
        //copytokd.setKdcode(); //金蝶编号
        copytokdout.setType(BillTypeEnums.OUT.getCode());
        copytokdout.setSubtype(WhoutformTypeEnums.SALE.getCode());
        copytokdout.setSellpaytype(sellform.getPaytype());
        copytokdout.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
        copytokdout.setSrguid(whoutform.getSrguid());
        copytokdout.setDepartmentid(whoutform.getDepartmentid());
        copytokdout.setParentid(parentid);
        //copytokd.setSrname();//库房没有
        //copytokd.setSrKdn();//库房金蝶编号没有
        copytokdout.setOrgguid(whoutform.getOrgguid());
        copytokdout.setOrgname(whoutform.getOrgname()); //工厂的组织id
        //copytokd.setOrgKdn();//没有组织编码金蝶编号
        copytokdout.setTotalqty(whoutform.getWeight());
        //copytokd.setTotalamount(whoutform.get);//没有总价
        copytokdout.setCurrency(whoutform.getCurrency());

        copytokdout.setKdsellpaytype(multipartConfig.getKdsellpaytype());

        copytokdout.setOperator(whoutform.getOperatorid());
        //copytokd.setOperatorKdn(); //没有操作人的金蝶编号
        copytokdout.setOrderdate(whoutform.getOuttime());
        copytokdout.setCopyTime(date);
        copytokdout.setLastCopyTime(date);
        copytokdout.setCreatetime(date);

        copytokdout.setIssplit(IssplitEnums.YES.getCode());


        List<Customer> clist = customerList.stream().filter(c -> (whoutform.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
        if (clist.size() == 1) {
            copytokdout.setCustomerid(clist.get(0).getGuid());
            copytokdout.setCustomername(clist.get(0).getName());
        } else {
            copytokdout.setCopyRemark(copytokdout.getCopyRemark() + "没有找到客户数据,");
            flag1 = false;
        }
        if (flag1) {
            copytokdout.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdout.setStatus(CopyStatusEnums.CANT.getCode());
        }

        this.copytokdService.insertCopytokd(copytokdout);
        Long copyid = copytokdout.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(copyid);
                    copytokditem.setSrguid(item.getSrguid());
                    copytokditem.setShrguid(item.getShrguid());
                    copytokditem.setOrgguid(item.getOrgguid());
                    copytokditem.setOrgname(item.getOrgname()); //工厂的组织id
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());
                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);
                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(whoutform.getOrgguid());
                    copytokditem.setOwner(whoutform.getOperatorid());
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());

                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());


                    List<Customer> clistitem = customerList.stream().filter(c -> (whoutform.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
                    if (clistitem.size() == 1) {
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());

                        copytokditem.setBalancecustomerid(clistitem.get(0).getGuid());
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());
                        copytokditem.setPaymentcustomerid(clistitem.get(0).getGuid());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到客户数据,");
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }

                    this.copytokditemService.insertCopytokditem(copytokditem);

                }

            }
        }

        //集团到工厂入库
        //供应商是工厂，没有客户
        getI(whoutform.getBusinesscode());
        Copytokd copytokdin = new Copytokd();
        boolean flag2 = true;
        copytokdin.setCopyRemark("");
        copytokdin.setLinkedautoid(whoutform.getAutoid());
        copytokdin.setOrderguid(whoutform.getGuid());
        copytokdin.setCode(sellform.getSellcode());
        copytokdin.setLinkedcode(whoutform.getWhoutcode());
        copytokdin.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdin.setType(BillTypeEnums.IN.getCode());
        copytokdin.setSubtype(WhinformTypeEnums.PURCHASING.getCode());
        copytokdin.setSellpaytype(sellform.getPaytype());
        copytokdin.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
        copytokdin.setCurrency(whoutform.getCurrency());
        copytokdin.setDepartmentid(whoutform.getDepartmentid());
        copytokdin.setParentid(parentid);
        //copytokdin.setSrguid(whoutform.getSrguid());
        List<Storeroom> storerooms = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storerooms.size() == 1) {
            copytokdin.setSrguid(storerooms.get(0).getGuid());
        } else {
            copytokdin.setCopyRemark(copytokdin.getCopyRemark() + "没有找到库房数据,");
            flag2 = false;
        }
        List<Supplier> slist = supplierList.stream().filter(s -> (whoutform.getOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (slist.size() == 1) {
            copytokdin.setSupplierid(slist.get(0).getId());
        } else {
            copytokdin.setCopyRemark(copytokdin.getCopyRemark() + "没有找到供应商数据,");
            flag2 = false;
        }

        copytokdin.setOrgguid(whoutform.getSettlOrgId());
        copytokdin.setTotalqty(whoutform.getWeight());
        copytokdin.setStatus(CopyStatusEnums.INIT.getCode());
        copytokdin.setOperator(whoutform.getOperatorid());
        copytokdin.setOrderdate(whoutform.getOuttime());
        copytokdin.setCopyTime(date);
        copytokdin.setLastCopyTime(date);
        copytokdin.setCreatetime(date);
        copytokdin.setIssplit(IssplitEnums.YES.getCode());

        if (flag2) {
            copytokdin.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdin.setStatus(CopyStatusEnums.CANT.getCode());
        }

        this.copytokdService.insertCopytokd(copytokdin);
        Long copyiid = copytokdin.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(copyiid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else {
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                        flagitem = false;
                    }
                    copytokditem.setOrgguid(whoutform.getSettlOrgId());
                    copytokditem.setOrgname(multipartConfig.getOrgname());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());
                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);
                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(multipartConfig.getMultipartOrgguid());
                    copytokditem.setOwner(whoutform.getOperatorid());

                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    List<Supplier> suppliersitem = supplierList.stream().filter(s -> whoutform.getOrgguid().equals(s.getOrgguid())).collect(Collectors.toList());
                    if (suppliersitem.size() == 1) {
                        copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
                        copytokditem.setSupplierid(suppliersitem.get(0).getId());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到供应商数据,");
                        flagitem = false;
                    }


                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }


                    this.copytokditemService.insertCopytokditem(copytokditem);
                }

            }
        }


        //销售组织到客户的出库
        getI(whoutform.getBusinesscode());
        Copytokd copytokdOutCus = new Copytokd();
        boolean flag3 = true;
        copytokdOutCus.setCopyRemark("");
        copytokdOutCus.setLinkedautoid(whoutform.getAutoid());
        copytokdOutCus.setOrderguid(whoutform.getGuid());
        copytokdOutCus.setCode(sellform.getSellcode());
        copytokdOutCus.setLinkedcode(whoutform.getWhoutcode());
        copytokdOutCus.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdOutCus.setType(BillTypeEnums.OUT.getCode());
        copytokdOutCus.setSubtype(WhoutformTypeEnums.SALE.getCode());
        copytokdOutCus.setSellpaytype(sellform.getPaytype());
        copytokdOutCus.setDepartmentid(whoutform.getDepartmentid());
        copytokdOutCus.setParentid(parentid);
        if (sellform.getPaytype().equals(5) || sellform.getPaytype().equals(4)  || sellform.getPaytype().equals(6)) {
            copytokdOutCus.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        } else {
            copytokdOutCus.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
        }
        copytokdOutCus.setCurrency(whoutform.getCurrency());
        List<Storeroom> storeroomos = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storeroomos.size() == 1) {
            copytokdOutCus.setSrguid(storeroomos.get(0).getGuid());
        } else {
            copytokdOutCus.setCopyRemark(copytokdOutCus.getCopyRemark() + "没有找到库房数据,");
            flag3 = false;
        }

        copytokdOutCus.setOrgguid(whoutform.getSettlOrgId());

        copytokdOutCus.setKdsellpaytype(multipartConfig.getKdsellpaytype());


        copytokdOutCus.setTotalqty(whoutform.getWeight());


        copytokdOutCus.setOperator(whoutform.getOperatorid());

        copytokdOutCus.setOrderdate(whoutform.getOuttime());
        copytokdOutCus.setCopyTime(date);
        copytokdOutCus.setLastCopyTime(date);
        copytokdOutCus.setCreatetime(date);
        copytokdOutCus.setIssplit(IssplitEnums.YES.getCode());
        if (!sellform.getPaytype().equals(5)) {
            copytokdOutCus.setCustomerid(whoutform.getRevCustId());
        }

        if (flag3) {
            copytokdOutCus.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdOutCus.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdOutCus);
        Long ocopyiid = copytokdOutCus.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(ocopyiid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else {
                            storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                        flagitem = false;
                    }
                    copytokditem.setOrgguid(whoutform.getSettlOrgId());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());

                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());
//
                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "2",sellform);

                    if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5)) {
                        copytokditem.setIspresent(IsPresentKDEnums.TRUE.getCode());
                    } else {
                        copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    }
                    copytokditem.setSaleorgid(multipartConfig.getMultipartOrgguid());

                    copytokditem.setOwner(whoutform.getOperatorid());

                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());


                    if (!sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(4) && !sellform.getPaytype().equals(6)) {
                        copytokditem.setOrdercustomerid(whoutform.getRevCustId());

                        copytokditem.setBalancecustomerid(whoutform.getRevCustId());
                        copytokditem.setOrdercustomerid(whoutform.getRevCustId());
                        copytokditem.setPaymentcustomerid(whoutform.getRevCustId());
                    }


                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                    this.copytokditemService.insertCopytokditem(copytokditem);

                    if (item.getHandselweight() != null) {
                        if (item.getHandselweight().compareTo(BigDecimal.ZERO) != 0) {
                            boolean flagitem1 = true;
                            Copytokditem copytokditem1 = new Copytokditem();
                            copytokditem1.setCopyRemark("");
                            copytokditem1.setCtkdid(ocopyiid);
                            List<Storeroom> storeroomitemzs = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                            if (storeroomitemzs.size() == 1) {
                                copytokditem1.setSrguid(storeroomitemzs.get(0).getGuid());
                                List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitemzs.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                                if (sellform.getQuitsrtype() == 1) {
                                    //原材料
                                    storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                } else if (sellform.getQuitsrtype() == 2) {
                                    //成品
                                    storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                } else {
                                    storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                }
                            } else {
                                copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                                flagitem1 = false;
                            }
                            copytokditem1.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());
                            copytokditem1.setOrgguid(whoutform.getSettlOrgId());
                            copytokditem1.setProductguid(item.getProductguid());
                            copytokditem1.setSpguid(item.getSpguid());

                            getQty(copytokditem1, item, IsPresentKDEnums.TRUE.getCode(), "2",sellform);

                            copytokditem1.setIspresent(IsPresentKDEnums.TRUE.getCode());
                            copytokditem1.setSaleorgid(multipartConfig.getMultipartOrgguid());

                            copytokditem1.setOwner(whoutform.getOperatorid());

                            copytokditem1.setStatus(CopyStatusEnums.INIT.getCode());
                            copytokditem1.setUnit(item.getStockunit());
                            copytokditem1.setUnitid(item.getStockunitid());

                            if (!sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(4) && !sellform.getPaytype().equals(6)) {

                                copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                                copytokditem1.setBalancecustomerid(whoutform.getRevCustId());
                                copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                                copytokditem1.setPaymentcustomerid(whoutform.getRevCustId());
                            }


                            copytokditem1.setCopyTime(date);
                            copytokditem1.setLastCopyTime(date);
                            copytokditem1.setCreatedate(date);
                            copytokditem1.setLastupdatetime(date);

                            if (flagitem1) {
                                copytokditem1.setStatus(CopyStatusEnums.INIT.getCode());
                            } else {
                                copytokditem1.setStatus(CopyStatusEnums.CANT.getCode());
                            }

                            this.copytokditemService.insertCopytokditem(copytokditem1);
                        }
                    }

                }

            }
        }
    }

    public void chaiAll(Whoutform whoutform, Sellform sellform, Date date, MultipartConfig multipartConfig, Long parentid) {
        //工厂到集团出库
        Copytokd copytokdout = new Copytokd();
        getI(whoutform.getBusinesscode());
        boolean flag1 = true;
        copytokdout.setCopyRemark("");
        copytokdout.setLinkedautoid(whoutform.getAutoid());
        copytokdout.setOrderguid(whoutform.getGuid());
        copytokdout.setCode(sellform.getSellcode());
        copytokdout.setLinkedcode(whoutform.getWhoutcode());
        copytokdout.setOrgguid(whoutform.getOrgguid());
        copytokdout.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdout.setType(BillTypeEnums.OUT.getCode());
        copytokdout.setSubtype(WhoutformTypeEnums.SALE.getCode());
        copytokdout.setSellpaytype(sellform.getPaytype());
        copytokdout.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
        copytokdout.setSrguid(whoutform.getSrguid());
        copytokdout.setCurrency(whoutform.getCurrency());
        copytokdout.setDepartmentid(whoutform.getDepartmentid());
        copytokdout.setParentid(parentid);

        copytokdout.setKdsellpaytype(multipartConfig.getKdsellpaytype());

        copytokdout.setTotalqty(whoutform.getWeight());


        copytokdout.setOperator(whoutform.getOperatorid());

        copytokdout.setOrderdate(whoutform.getOuttime());
        copytokdout.setCopyTime(date);
        copytokdout.setLastCopyTime(date);
        copytokdout.setCreatetime(date);
        copytokdout.setIssplit(IssplitEnums.YES.getCode());


        List<Customer> outclist = customerList.stream().filter(c -> (multipartConfig.getMultipartOrgguid().equals(c.getOrgguid()))).collect(Collectors.toList());
        if (outclist.size() == 1) {
            copytokdout.setCustomerid(outclist.get(0).getGuid());
        } else {
            copytokdout.setCopyRemark(copytokdout.getCopyRemark() + "没有找到客户数据,");
            flag1 = false;
        }
        if (flag1) {
            copytokdout.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdout.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdout);
        Long copyid = copytokdout.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(copyid);
                    copytokditem.setSrguid(item.getSrguid());
                    copytokditem.setShrguid(item.getShrguid());
                    copytokditem.setOrgguid(item.getOrgguid());

                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());

                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);

                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(whoutform.getOrgguid());

                    copytokditem.setOwner(whoutform.getOperatorid());

                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());

                    List<Customer> clistitem = customerList.stream().filter(c -> (multipartConfig.getMultipartOrgguid().equals(c.getOrgguid()))).collect(Collectors.toList());
                    if (clistitem.size() == 1) {
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());

                        copytokditem.setBalancecustomerid(clistitem.get(0).getGuid());
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());
                        copytokditem.setPaymentcustomerid(clistitem.get(0).getGuid());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到客户数据,");
                        flagitem = false;
                    }

                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                    this.copytokditemService.insertCopytokditem(copytokditem);
                }
            }
        }

        //集团到工厂入库
        //供应商是工厂,没有客户
        getI(whoutform.getBusinesscode());
        Copytokd copytokdin = new Copytokd();
        boolean flag2 = true;
        copytokdin.setCopyRemark("");
        copytokdin.setLinkedautoid(whoutform.getAutoid());
        copytokdin.setOrderguid(whoutform.getGuid());
        copytokdin.setCode(sellform.getSellcode());
        copytokdin.setLinkedcode(whoutform.getWhoutcode());
        copytokdin.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdin.setType(BillTypeEnums.IN.getCode());
        copytokdin.setSubtype(WhinformTypeEnums.PURCHASING.getCode());
        copytokdin.setSellpaytype(sellform.getPaytype());
        copytokdin.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
        copytokdin.setCurrency(whoutform.getCurrency());
        copytokdin.setDepartmentid(whoutform.getDepartmentid());
        copytokdin.setParentid(parentid);
        List<Supplier> inslist = supplierList.stream().filter(s -> (whoutform.getOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (inslist.size() == 1) {
            copytokdin.setSupplierid(inslist.get(0).getId());
        } else {
            copytokdin.setCopyRemark(copytokdin.getCopyRemark() + "没有找到供应商数据,");
            flag2 = false;
        }
        List<Storeroom> storerooms = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && multipartConfig.getMultipartOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storerooms.size() == 1) {
            copytokdin.setSrguid(storerooms.get(0).getGuid());
        } else {
            copytokdin.setCopyRemark(copytokdin.getCopyRemark() + "没有找到库房数据,");
            flag2 = false;
        }

        copytokdin.setOrgguid(multipartConfig.getMultipartOrgguid());
        copytokdin.setOrgname(multipartConfig.getOrgname());

        copytokdin.setTotalqty(whoutform.getWeight());

        copytokdin.setOperator(whoutform.getOperatorid());

        copytokdin.setOrderdate(whoutform.getOuttime());
        copytokdin.setCopyTime(date);
        copytokdin.setLastCopyTime(date);
        copytokdin.setCreatetime(date);
        copytokdin.setIssplit(IssplitEnums.YES.getCode());

        if (flag2) {
            copytokdin.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdin.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdin);
        Long copyiid = copytokdin.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(copyiid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && multipartConfig.getMultipartOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else {
                            storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据");
                        flagitem = false;
                    }
                    copytokditem.setOrgguid(multipartConfig.getMultipartOrgguid());
                    copytokditem.setOrgname(multipartConfig.getOrgname());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());

                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);

                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(multipartConfig.getMultipartOrgguid());

                    copytokditem.setOwner(whoutform.getOperatorid());
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    List<Supplier> suppliersitem = supplierList.stream().filter(s -> whoutform.getOrgguid().equals(s.getOrgguid())).collect(Collectors.toList());
                    if (suppliersitem.size() == 1) {
                        copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
                        copytokditem.setSupplierid(suppliersitem.get(0).getId());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到供应商数据,");
                        flagitem = false;
                    }
                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                    this.copytokditemService.insertCopytokditem(copytokditem);
                }

            }
        }


        //集团到销售出库
        getI(whoutform.getBusinesscode());
        Copytokd copytokdsaleout = new Copytokd();
        boolean flag3 = true;
        copytokdsaleout.setCopyRemark("");
        copytokdsaleout.setLinkedautoid(whoutform.getAutoid());
        copytokdsaleout.setOrderguid(whoutform.getGuid());
        copytokdsaleout.setCode(sellform.getSellcode());
        copytokdsaleout.setLinkedcode(whoutform.getWhoutcode());
        copytokdsaleout.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdsaleout.setType(BillTypeEnums.OUT.getCode());
        copytokdsaleout.setSubtype(WhoutformTypeEnums.SALE.getCode());
        copytokdsaleout.setSellpaytype(sellform.getPaytype());
        copytokdsaleout.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
        copytokdsaleout.setOrgguid(multipartConfig.getMultipartOrgguid());
        copytokdsaleout.setOrgname(multipartConfig.getOrgname());
        copytokdsaleout.setCurrency(whoutform.getCurrency());
        copytokdsaleout.setDepartmentid(whoutform.getDepartmentid());
        copytokdsaleout.setParentid(parentid);

        copytokdsaleout.setKdsellpaytype(multipartConfig.getKdsellpaytype());

        List<Storeroom> storeroomouts = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && multipartConfig.getMultipartOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storeroomouts.size() == 1) {
            copytokdsaleout.setSrguid(storeroomouts.get(0).getGuid());
        } else {
            copytokdsaleout.setCopyRemark(copytokdsaleout.getCopyRemark() + "没有找到库房数据,");
            flag3 = false;
        }

        copytokdsaleout.setOrgguid(multipartConfig.getMultipartOrgguid());
        copytokdsaleout.setOrgname(multipartConfig.getOrgname()); //工厂的组织id

        copytokdsaleout.setTotalqty(whoutform.getWeight());

        copytokdsaleout.setOperator(whoutform.getOperatorid());

        copytokdsaleout.setOrderdate(whoutform.getOuttime());
        copytokdsaleout.setCopyTime(date);
        copytokdsaleout.setLastCopyTime(date);
        copytokdsaleout.setCreatetime(date);
        copytokdsaleout.setIssplit(IssplitEnums.YES.getCode());
        List<Customer> saleclist = customerList.stream().filter(c -> (whoutform.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
        if (saleclist.size() == 1) {
            copytokdsaleout.setCustomerid(saleclist.get(0).getGuid());
        } else {
            copytokdsaleout.setCopyRemark(copytokdsaleout.getCopyRemark() + "没有找到客户数据,");
            flag3 = false;
        }

        if (flag3) {
            copytokdsaleout.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdsaleout.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdsaleout);
        Long salecopyid = copytokdsaleout.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(salecopyid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && multipartConfig.getMultipartOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else {
                            storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                        flagitem = false;
                    }

                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());
                    copytokditem.setOrgguid(multipartConfig.getMultipartOrgguid());
                    copytokditem.setOrgname(multipartConfig.getOrgname()); //工厂的组织id
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());
                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);
                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(whoutform.getOrgguid());
                    copytokditem.setOwner(whoutform.getOperatorid());

                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    List<Customer> clistitem = customerList.stream().filter(c -> (whoutform.getSettlOrgId().equals(c.getOrgguid()))).collect(Collectors.toList());
                    if (clistitem.size() == 1) {
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());

                        copytokditem.setBalancecustomerid(clistitem.get(0).getGuid());
                        copytokditem.setOrdercustomerid(clistitem.get(0).getGuid());
                        copytokditem.setPaymentcustomerid(clistitem.get(0).getGuid());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到客户数据,");
                        flagitem = false;
                    }

                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }


                    this.copytokditemService.insertCopytokditem(copytokditem);
                }

            }
        }


        //销售到集团入库
        //供应商是集团， 没有客户
        getI(whoutform.getBusinesscode());
        Copytokd copytokdsalein = new Copytokd();
        copytokdsalein.setCopyRemark("");
        boolean flag4 = true;
        copytokdsalein.setLinkedautoid(whoutform.getAutoid());
        copytokdsalein.setOrderguid(whoutform.getGuid());
        copytokdsalein.setCode(sellform.getSellcode());
        copytokdsalein.setLinkedcode(whoutform.getWhoutcode());
        copytokdsalein.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdsalein.setType(BillTypeEnums.IN.getCode());
        copytokdsalein.setSubtype(WhinformTypeEnums.PURCHASING.getCode());
        copytokdsalein.setSellpaytype(sellform.getPaytype());
        copytokdsalein.setKdtype(KDWhinformTypeEnums.PURCHASING.getCode());
        copytokdsalein.setCurrency(whoutform.getCurrency());
        copytokdsalein.setDepartmentid(whoutform.getDepartmentid());
        copytokdsalein.setParentid(parentid);
        List<Supplier> saleinslist = supplierList.stream().filter(s -> (multipartConfig.getMultipartOrgguid().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (saleinslist.size() == 1) {
            copytokdsalein.setSupplierid(saleinslist.get(0).getId());
        } else {
            copytokdsalein.setCopyRemark(copytokdsalein.getCopyRemark() + "没有找到供应商数据,");
            flag4 = false;
        }
        List<Storeroom> storeroomins = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storeroomins.size() == 1) {
            copytokdsalein.setSrguid(storeroomins.get(0).getGuid());
        } else {
            copytokdsalein.setCopyRemark(copytokdsalein.getCopyRemark() + "没有找到库房数据,");
            flag4 = false;
        }

        copytokdsalein.setOrgguid(whoutform.getSettlOrgId());
        copytokdsalein.setTotalqty(whoutform.getWeight());
        copytokdsalein.setOperator(whoutform.getOperatorid());
        copytokdsalein.setOrderdate(whoutform.getOuttime());
        copytokdsalein.setCopyTime(date);
        copytokdsalein.setLastCopyTime(date);
        copytokdsalein.setCreatetime(date);
        copytokdsalein.setIssplit(IssplitEnums.YES.getCode());

        if (flag4) {
            copytokdsalein.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdsalein.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdsalein);
        Long salecopyiid = copytokdsalein.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(salecopyiid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }

                        } else {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                        flagitem = false;
                    }
                    copytokditem.setOrgguid(whoutform.getSettlOrgId());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());

                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "1",sellform);
                    copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    copytokditem.setSaleorgid(multipartConfig.getMultipartOrgguid());
                    copytokditem.setOwner(whoutform.getOperatorid());

                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());

                    List<Supplier> suppliersitem = supplierList.stream().filter(s -> multipartConfig.getMultipartOrgguid().equals(s.getOrgguid())).collect(Collectors.toList());
                    if (suppliersitem.size() == 1) {
                        copytokditem.setBalancesupplierid(suppliersitem.get(0).getId());
                        copytokditem.setSupplierid(suppliersitem.get(0).getId());
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到供应商数据,");
                        flagitem = false;
                    }

                    if (item.getLoadcount() != null) {
                        copytokditem.setLoadcount(new BigDecimal(item.getLoadcount()));
                    } else {
                        copytokditem.setLoadcount(BigDecimal.ZERO);
                    }

                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }


                    this.copytokditemService.insertCopytokditem(copytokditem);
                }
            }
        }

        //销售组织到客户的出库
        getI(whoutform.getBusinesscode());
        Copytokd copytokdOutCus = new Copytokd();
        boolean flag5 = true;
        copytokdOutCus.setCopyRemark("");
        copytokdOutCus.setLinkedautoid(whoutform.getAutoid());
        copytokdOutCus.setOrderguid(whoutform.getGuid());
        copytokdOutCus.setCode(sellform.getSellcode());
        copytokdOutCus.setLinkedcode(whoutform.getWhoutcode());
        copytokdOutCus.setKdcode(whoutform.getBusinesscode() + "A" + i);
        copytokdOutCus.setType(BillTypeEnums.OUT.getCode());
        copytokdOutCus.setSubtype(WhoutformTypeEnums.SALE.getCode());
        copytokdOutCus.setSellpaytype(sellform.getPaytype());
        copytokdOutCus.setDepartmentid(whoutform.getDepartmentid());
        copytokdOutCus.setParentid(parentid);

        //样品---其他出库单
        //赠送或促销----明细都赠送
        if (sellform.getPaytype().equals(5) || sellform.getPaytype().equals(4) || sellform.getPaytype().equals(6)) {
            copytokdOutCus.setKdtype(KDWhoutformTypeEnums.OTHER.getCode());
        } else {
            copytokdOutCus.setKdtype(KDWhoutformTypeEnums.SALE.getCode());
        }
        copytokdOutCus.setCurrency(whoutform.getCurrency());

        List<Storeroom> storeroomcus = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storeroomcus.size() == 1) {
            copytokdOutCus.setSrguid(storeroomcus.get(0).getGuid());
        } else {
            copytokdOutCus.setCopyRemark(copytokdOutCus.getCopyRemark() + "没有找到库房数据,");
            flag5 = false;
        }

        copytokdOutCus.setKdsellpaytype(multipartConfig.getKdsellpaytype());

        copytokdOutCus.setOrgguid(whoutform.getSettlOrgId());
        copytokdOutCus.setTotalqty(whoutform.getWeight());
        copytokdOutCus.setOperator(whoutform.getOperatorid());
        copytokdOutCus.setOrderdate(whoutform.getOuttime());
        copytokdOutCus.setCopyTime(date);
        copytokdOutCus.setLastCopyTime(date);
        copytokdOutCus.setCreatetime(date);
        copytokdOutCus.setIssplit(IssplitEnums.YES.getCode());
        if (!sellform.getPaytype().equals(5)) {
            copytokdOutCus.setCustomerid(whoutform.getRevCustId());
        }

        if (flag5) {
            copytokdOutCus.setStatus(CopyStatusEnums.INIT.getCode());
        } else {
            copytokdOutCus.setStatus(CopyStatusEnums.CANT.getCode());
        }
        this.copytokdService.insertCopytokd(copytokdOutCus);
        Long ocopyiid = copytokdOutCus.getId();
        if (whoutform.getWhoutformitemList() != null) {
            for (Whoutformitem item : whoutform.getWhoutformitemList()) {
                if (item.getWeight().compareTo(BigDecimal.ZERO) != 0) {
                    boolean flagitem = true;
                    Copytokditem copytokditem = new Copytokditem();
                    copytokditem.setCopyRemark("");
                    copytokditem.setCtkdid(ocopyiid);
                    List<Storeroom> storeroomitems = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                    if (storeroomitems.size() == 1) {
                        copytokditem.setSrguid(storeroomitems.get(0).getGuid());
                        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitems.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                        if (sellform.getQuitsrtype() == 1) {
                            //原材料
                            storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else if (sellform.getQuitsrtype() == 2) {
                            //成品
                            storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        } else {
                            storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                copytokditem.setShrguid(storehouserooms.get(0).getGuid());
                            } else {
                                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                flagitem = false;
                            }
                        }
                    } else {
                        copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                        flagitem = false;
                    }

                    copytokditem.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());
                    copytokditem.setOrgguid(whoutform.getSettlOrgId());
                    copytokditem.setProductguid(item.getProductguid());
                    copytokditem.setSpguid(item.getSpguid());

                    getQty(copytokditem, item, IsPresentKDEnums.FALSE.getCode(), "2",sellform);
                    //赠送或促销----明细都赠送
                    if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(6)) {
                        copytokditem.setIspresent(IsPresentKDEnums.TRUE.getCode());
                    } else {
                        copytokditem.setIspresent(IsPresentKDEnums.FALSE.getCode());
                    }

                    copytokditem.setSaleorgid(multipartConfig.getMultipartOrgguid());
                    copytokditem.setOwner(whoutform.getOperatorid());
                    copytokditem.setStatus(CopyStatusEnums.INIT.getCode());


                    if (!sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(6) && !sellform.getPaytype().equals(4)) {
                        copytokditem.setOrdercustomerid(whoutform.getRevCustId());

                        copytokditem.setBalancecustomerid(whoutform.getRevCustId());
                        copytokditem.setOrdercustomerid(whoutform.getRevCustId());
                        copytokditem.setPaymentcustomerid(whoutform.getRevCustId());
                    }

                    copytokditem.setUnit(item.getStockunit());
                    copytokditem.setUnitid(item.getStockunitid());


                    copytokditem.setCopyTime(date);
                    copytokditem.setLastCopyTime(date);
                    copytokditem.setCreatedate(date);
                    copytokditem.setLastupdatetime(date);

                    if (flagitem) {
                        copytokditem.setStatus(CopyStatusEnums.INIT.getCode());
                    } else {
                        copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                    this.copytokditemService.insertCopytokditem(copytokditem);

                    if (item.getHandselweight() != null) {
                        if (item.getHandsel().compareTo(BigDecimal.ZERO) != 0) {
                            boolean flagitem1 = true;
                            Copytokditem copytokditem1 = new Copytokditem();
                            copytokditem1.setCtkdid(ocopyiid);
                            List<Storeroom> storeroomitemzs = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && whoutform.getSettlOrgId().equals(s.getOrgguid()))).collect(Collectors.toList());
                            if (storeroomitemzs.size() == 1) {
                                copytokditem1.setSrguid(storeroomitemzs.get(0).getGuid());
                                List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeroomitemzs.get(0).getGuid().equals(s.getStoreguid()))).collect(Collectors.toList());
                                if (sellform.getQuitsrtype() == 1) {
                                    //原材料
                                    storehouserooms = storehouserooms.stream().filter(s -> 1 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                } else if (sellform.getQuitsrtype() == 2) {
                                    //成品
                                    storehouserooms = storehouserooms.stream().filter(s -> 2 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                } else {
                                    storehouserooms = storehouserooms.stream().filter(s -> 3 == s.getShrtype()).collect(Collectors.toList());
                                    if (storehouserooms.size() == 1) {
                                        copytokditem1.setShrguid(storehouserooms.get(0).getGuid());
                                    } else {
                                        copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库间数据,");
                                        flagitem1 = false;
                                    }
                                }
                            } else {
                                copytokditem1.setCopyRemark(copytokditem.getCopyRemark() + "没有找到库房数据,");
                                flagitem1 = false;
                            }

                            copytokditem1.setKdupdatetype(KdUpdateTypeEnums.COMMONOUT.getCode());
                            copytokditem1.setOrgguid(whoutform.getSettlOrgId());
                            copytokditem1.setProductguid(item.getProductguid());
                            copytokditem1.setSpguid(item.getSpguid());
                            getQty(copytokditem1, item, IsPresentKDEnums.TRUE.getCode(), "2",sellform);
                            copytokditem1.setIspresent(IsPresentKDEnums.TRUE.getCode());
                            copytokditem1.setSaleorgid(whoutform.getOrgguid());
                            copytokditem1.setOwner(whoutform.getOperatorid());
                            copytokditem1.setStatus(CopyStatusEnums.INIT.getCode());
                            copytokditem1.setUnit(item.getStockunit());
                            copytokditem1.setUnitid(item.getStockunitid());


                            if (!sellform.getPaytype().equals(5) && !sellform.getPaytype().equals(6) && !sellform.getPaytype().equals(4)) {
                                copytokditem1.setOrdercustomerid(whoutform.getRevCustId());

                                copytokditem1.setBalancecustomerid(whoutform.getRevCustId());
                                copytokditem1.setOrdercustomerid(whoutform.getRevCustId());
                                copytokditem1.setPaymentcustomerid(whoutform.getRevCustId());
                            }


                            copytokditem1.setCopyTime(date);
                            copytokditem1.setLastCopyTime(date);
                            copytokditem1.setCreatedate(date);
                            copytokditem1.setLastupdatetime(date);

                            if (flagitem1) {
                                copytokditem1.setStatus(CopyStatusEnums.INIT.getCode());
                            } else {
                                copytokditem1.setStatus(CopyStatusEnums.CANT.getCode());
                            }

                            this.copytokditemService.insertCopytokditem(copytokditem1);
                        }
                    }

                }

            }
        }


    }


}
